Nearby lessons

82 of 159

Python - Nested Try Except

📌 What You Will Learn
  • Write try-except-finally blocks inside another try, except, or finally block
  • Know that the inner except block gets the first opportunity to handle inner exceptions
  • Trace how an unhandled inner exception propagates to the outer except block
  • Understand that finally always runs once control enters a try block

Nested try-except-finally Blocks

We can write try-except-finally blocks inside another try, except, or finally block.

This concept is called Nested try-except-finally.

Nested exception handling helps organize complex programs with multiple risky operations.

General Structure

🐍Code Cell
1try:
2 ----------
3 ----------
4 ----------
5 
6 try:
7 ----------
8 ----------
9 ----------
10 
11 except:
12 ----------
13 ----------
14 ----------
15 
16except:
17 ----------
18 ----------
19 ----------
Output
No output captured.

Important Points

  • General risky code should be placed inside the outer try block.
  • More risky code should be placed inside the inner try block.
  • If an exception occurs inside the inner try block, the inner except block is responsible for handling it.
  • If the inner except block cannot handle the exception, then the outer except block is responsible for handling it.

Example

🐍Code Cell
1try:
2 print("outer try block")
3 
4 try:
5 print("Inner try block")
6 print(10/0)
7 
8 except ZeroDivisionError:
9 print("Inner except block")
10 
11 finally:
12 print("Inner finally block")
13 
14except:
15 print("outer except block")
16 
17finally:
18 print("outer finally block")
Output
outer try block
Inner try block
Inner except block
Inner finally block
outer finally block

Explanation

  1. The outer try block starts execution and prints outer try block.
  2. The inner try block starts and prints Inner try block.
  3. print(10/0) raises a ZeroDivisionError.
  4. The inner except ZeroDivisionError block handles it and prints Inner except block.
  5. The inner finally block runs and prints Inner finally block.
  6. Since the exception was already handled, the outer except block is skipped.
  7. Finally, the outer finally block runs and prints outer finally block.

Key Observations

  1. The inner try block gets the first opportunity to execute.
  2. The inner except block gets the first opportunity to handle exceptions raised by the inner try.
  3. Once the exception is handled by the inner except, it is not propagated to the outer except.
  4. The inner finally block always executes after the inner try.
  5. The outer finally block always executes after the outer try.

Control Flow in Nested try-except-finally

The control flow of nested try-except-finally blocks is represented as follows:

🐍Code Cell
1try:
2 stmt-1
3 stmt-2
4 stmt-3
5 
6 try:
7 stmt-4
8 stmt-5
9 stmt-6
10 
11 except X:
12 stmt-7
13 
14 finally:
15 stmt-8
16 
17 stmt-9
18 
19except Y:
20 stmt-10
21 
22finally:
23 stmt-11
24 
25stmt-12
Output
No output captured.

Main Execution Cases

Case Execution Flow Result
No exception anywhere 1 → 2 → 3 → 4 → 5 → 6 → 8 → 9 → 11 → 12 Normal Termination
Exception handled by inner except 1 → 2 → 3 → 4 → 7 → 8 → 9 → 11 → 12 Normal Termination
Exception in inner try, not handled by inner except 1 → 2 → 3 → 4 → 8 → 10 → 11 → 12 Normal Termination
Exception in inner try, no outer handler either 1 → 2 → 3 → 4 → 8 → 11 Abnormal Termination
Exception raised inside the finally block 1 → ... → exception in finally Abnormal Termination

Important Note

  • If the control enters the try block, then the corresponding finally block will always be executed.
  • If the control does not enter the try block, then the corresponding finally block will not be executed.
📝 Key Takeaways
  • Nesting means writing try-except-finally inside another try, except, or finally block
  • The inner except block gets the first chance to handle exceptions raised in the inner try
  • If the inner except cannot handle the exception, the outer except gets the next chance
  • Once the inner except handles an exception, it does not propagate to the outer except
  • If control enters a try block, its corresponding finally block always executes

🧠 Test Your Knowledge

7 Questions
Progress: 0 / 7