Finally block runs just before the return statement in the try block, as shown in the below example - returns False
instead of True
:
>>> def bool_return(): ... try: ... return True ... finally: ... return False ... >>> bool_return() False
Similarly, the following code returns value set in the Finally block:
>>> def num_return(): ... try: ... x=100 ... return x ... finally: ... x=90 ... return x ... >>> num_return() 90
However, for variable assignment without return statement in the finally block, why does value of variable updated by the finally block not get returned by the try block? Is the variable from finally block scoped locally in the finally block? Or is the return value from the try block held in memory buffer and unaffected by assignment in finally block? In the below example, why is the output 100 instead of 90?
>>> def num_return(): ... try: ... x=100 ... return x ... finally: ... x=90 ... >>> num_return() 100
Similarly the following example:
In [1]: def num_return(): ...: try: ...: x=[100] ...: return x ...: finally: ...: x[0] = 90 ...: In [2]: num_return() Out[2]: [90] In [3]: def num_return(): ...: try: ...: x=[100] ...: return x[0] ...: finally: ...: x[0] = 90 ...: In [4]: num_return() Out[4]: 100
finally trumps return. This is because when return i; is executed i has a value 2. After this the finally block is executed where 12 is assigned to i and then System.
Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.
A finally block is always get executed whether the exception has occurred or not. If an exception occurs like closing a file or DB connection, then the finally block is used to clean up the code. We cannot say the finally block is always executes because sometimes if any statement like System.
What Is finally? finally defines a block of code we use along with the try keyword. It defines code that's always run after the try and any catch block, before the method is completed. The finally block executes regardless of whether an exception is thrown or caught.
A little experiment to help confirm what others have answered, is to replace x
with a single-value list, like this:
def num_return(): try: x=[100] return x finally: x[0] = 90
now the value returned is [90]
, so the list is indeed modified in the finally block.
BUT if you return x[0]
, you get 100
(even though we just based the fact that the list itself does change in the finally block).
In [1]: def num_return(): ...: try: ...: x=[100] ...: return x ...: finally: ...: x[0] = 90 ...: In [2]: num_return() Out[2]: [90] In [3]: def num_return(): ...: try: ...: x=[100] ...: return x[0] ...: finally: ...: x[0] = 90 ...: In [4]: num_return() Out[4]: 100
When you say return x
, Python saves the value of the variable x
at that point as the return value. Updating x later doesn't affect this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With