Picture of Aravind Ramalingam

Aravind Ramalingam

  • Python
  • Exception
  • Function
  • Error Handling

Do You Really Understand Try & Finally in Python?

In python, try and except blocks are often used by programmers for handling any exception or unhappy scenarios. Finally clause is very under appreciated & can be better utilised. Let us check out how final-block works.

Lady confused about Try/Final block in python

Deep dive

This quote from the python documentation is absolutely correct but the execution behavior is little tricky when try and finally blocks are encapsulated within a function which has a return statement. Let me explain with examples. See if you could guess the output of the following functions.

Example 1:

# Both the try & final blocks have print statements and function returns value from final-block
def example_1():
    try:
        val = 1
        print(f"Print: Try Block - ")
    finally:
        val = val + 1
        print(f"Print: Finally Block - ")
        return f"Return: Finally Block - "
    
example_1()

Example 1 - output

Function example_1 is simple and straight, first the try-block gets executed and then final-block. The variable val has value 1 in try-block and gets updated to 2 in final-block.

Example 2:

# The try block has return statement & final block has only print statement
def example_2():
    try:
        val = 1
        print(f"Print: Try Block - ")
        return f"Return: Try Block - "
    finally:
        val = val + 1
        print(f"Print: Finally Block - ")
        
example_2()

Example 2 - output

Function example_2 is where things get a bit tricky, the returnstatement in try-block is executed after the final-block but the value of the variable valreturned is not affected by the changes made in the final-block.

Example 3:

# Both the try & final blocks have return statements
def example_3():
    try:
        val = 1
        print(f"Print: Try Block - ")
        return f"Return: Try Block - "
    finally:
        val = val + 1
        print(f"Print: Finally Block - ")
        return f"Return: Finally Block - "
    
example_3()

Example 3 - output

Output of the function example_3 is easy to guess. When the return statement in final-block is executed, the function exits so the return statement in try-block is never executed.

Take Away

Code Execution Flow

try & finallyblocks are not quite as straight forward as one might think, especially when they are returning values from a function. For more such interesting tidbits follow me via Twitter. The takeaways from this post are:

  1. Where you put the return statement is going to make a difference.
  2. Even though the return statement from try-block is executed after the final-block, value of the variable returned won't be affected by alteration made in the final-block.