python: How to check that returned value is exception or not

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



python: How to check that returned value is exception or not



I have two functions. function "main" is calling to function "submain". "submain" function contains try-catch block and return some value.



If "submain" executes successfully then it returns some value which i will display to end user directly.



If i get an exception then i have to prepare one user friendly message and print that. But to do that i need to know whether returned value is exception or not.



How can i check that returned value in exception or valid result?


Here is my pseudo code:
def submain():
try:
result = call to external API
return result
excepion, e:
_logger(e)

def main(value):
for x in value:
submain()
# if return is exception then break loop and give msg to end user.





Would you please put your code?
– mark keven
40 mins ago





Take a look at the section Best Practices: except clause in this answer. You can catch an exception at a lower level, do some required work and then re-raise it for the next upper level to catch and handle it again, if needed.
– shmee
19 mins ago


Best Practices: except clause





@markkeven: Added pseudo code.
– Ankit
just now




1 Answer
1



If you want to handle (try catch) the exception in function main then I think you should throw it from function submain with its context.
If you swallow the exception in submain function there no way main function could know that without some tricks.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard