Reputation: 33243
If I have a function something like:
def foo(.. ):
try:
#something
return_value = "bleh"
except Exception,e:
logging.error("exception " +e)
return return_value
Does the above look ok? I mean, if I got the exception then return_value
is never initialized.
What's a good way to handle exceptions in a function that has some return value?
Upvotes: 1
Views: 1278
Reputation: 1509
You should return a False or something of that kind to tell the caller that some error has occurred. And then handle this return value in your caller, like if return is False do something else.
def foo(.. ):
try:
#something
return_value = "bleh"
except Exception,e:
logging.error("exception " +e)
return_value = False
return return_value
Upvotes: 2