Nested try-except-finally blocks in Python

Nested try-except-finally blocks in Python

In this article, I am going to discuss Nested try-except-finally blocks in Python with examples. Please read our previous article where we discussed Finally Block in Python. As part of this article, we are going to discuss the following pointers in details.

  1. Nested try-except-finally blocks in Python
  2. Different cases and scenarios
  3. Else Block in Python
  4. Possible Combinations with try-except-else-finally
Nested try-except-finally blocks in Python

In python nested try except finally blocks are allowed. We can take try-except-finally blocks inside try block. We can take try-except-finally blocks inside except block. We can take try-except-finally blocks inside finally block

Different cases and scenarios

Case 1: If no exception raised then outer try, inner try, inner finally, outer finally blocks will get executed

Program: demo28.py

try:
   print("outer try block")
   try:
       print("Inner try block")
   except ZeroDivisionError:
       print("Inner except block")
   finally:
       print("Inner finally block")
except:
   print("outer except block")
finally:
   print("outer finally block")

Output:

If no exception raised then outer try, inner try, inner finally, outer finally blocks will get executed

Case 2:

If an exception is raised in an outer try, then outer except blocks are responsible to handle that exception.

Program: demo29.py

try:
   print("outer try block")
   print(10/0)
   try:
       print("Inner try block")
   except ZeroDivisionError:
       print("Inner except block")
   finally:
       print("Inner finally block")
except:
   print("outer except block")
finally:
   print("outer finally block")

Output:

If an exception is raised in an outer try, then outer except blocks are responsible to handle that exception.

Case 3:

If an exception raised in inner try block then inner except block is responsible to handle, if it is unable to handle then outer except block is responsible to handle.

Program: Exception handled by inner except block (demo30.py)

try:
   print("outer try block")
   try:
       print("Inner try block")
       print(10/0)
   except ZeroDivisionError:
       print("Inner except block")
   finally:
       print("Inner finally block")
except:
   print("outer except block")
finally:
   print("outer finally block")

Output:

Nested try-except-finally blocks in Python

Program: Exception handled by outer except block (demo31.py)

try:
   print("outer try block")
   try:
       print("Inner try block")
       print(10/0)
   except NameError:
       print("Inner except block")
   finally:
       print("Inner finally block")
except:
   print("outer except block")
finally:
   print("outer finally block")

Output:

Nested try-except-finally blocks in Python with Examples

Else Block in Python:

We can use else blocks with try-except-finally blocks. The else block will be executed if and only if there are no exceptions inside the try block.

Note: If no exception then try, else and finally blocks will get executed. If an exception is raised inside the try block, then the except block will get executed but the else block won’t.

Program: else block (demo32.py)

try:
   print("try block")
except:
   print("except: Handling code")
else:
   print("else block")
finally:
   print("finally block")

Output:

Else Block in Python

Program: else block (demo33.py)

try:
   print("try block")
   print(10/0)
except:
   print("except: Handling code")
else:
   print("else block")
finally:
   print("finally block")

Output:

Else Block in Python

Possible Combinations with try-except-else-finally

Only try block is invalid, only except block is invalid, only finally block is invalid

Program: only try block (demo33.py)

try:
   print("try block")

Output: only try block

Program: only except block (demo34.py)

except:
   print("except: Handling code")

Output: only except block

Program: Only Finally block (demo35.py)

finally:
   print("finally block")

Output: 

Note:
  1. try with except combination is valid → try block follows except block
  2. try with finally combination is valid → try block follows finally block
  3. try, except and else combination is valid → try, except blocks follows else block

Program: try with except combination (demo36.py)

try:
   print("try block")
except:
   print("except: Handling code")

Output: try with except combination

Program: try with finally combination (demo37.py)

try:
   print("try block")
finally:
   print("finally block")

Output:

try with finally combination

Program: try, except and else combination (demo38.py)

try:
   print("try block")

except:
   print("except: Handling code")
else:
   print("else block")

Output:

try, except and else combination

In the next article, I am going to discuss Custom Exceptions in Python with Examples. Here, in this article, I try to explain Nested try-except-finally blocks in Python with Examples. I hope you enjoy this Nested try-except-finally blocks in Python article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *