DEV Community

Cover image for Do You Really Understand Try & Finally in Python?
Aravind Ramalingam
Aravind Ramalingam

Posted on • Updated on • Originally published at Medium

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 utilized. Let us check out how final-block works.

Deep dive

No matter what happened previously, the final-block is executed once the code block is complete and any raised exceptions handled. Even if there's an error in an exception handler or the else-block and a new exception is raised, the code in the final-block is still run.

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 - {val}")
    finally:
        val = val + 1
        print(f"Print: Finally Block - {val}")
        return f"Return: Finally Block - {val}"

example_1()
Enter fullscreen mode Exit fullscreen mode

Alt Text

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 - {val}")
        return f"Return: Try Block - {val}"
    finally:
        val = val + 1
        print(f"Print: Finally Block - {val}")

example_2()
Enter fullscreen mode Exit fullscreen mode

Alt Text

Function example_2 is where things get a bit tricky, the return statement in try-block is executed after the final-block but the value of the variable val returned 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 - {val}")
        return f"Return: Try Block - {val}"
    finally:
        val = val + 1
        print(f"Print: Finally Block - {val}")
        return f"Return: Finally Block - {val}"

example_3()
Enter fullscreen mode Exit fullscreen mode

Alt Text

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

Alt Text

try & finally blocks 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.

Top comments (0)