I'm wondering why the function foo is returning 3 instead 1. Please explain.
def foo():
try:
return 1
except:
return 2
finally:
return 3
I'm wondering why the function foo is returning 3 instead 1. Please explain.
def foo():
try:
return 1
except:
return 2
finally:
return 3
The finally block executes regardless of Exceptions. Take a look at this question.
To elaborate, the finally
clause is always executed before the end of the try
statement. Here is the documentation.
Finally is always the last item to happen after executing a try block.
Yes, the finally block is executed however the flow leaves the try block - whether by reaching the end, returning, or throwing an exception.