Nearby lessons
82 of 159Python - 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
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
tryblock, the innerexceptblock is responsible for handling it. - If the inner
exceptblock cannot handle the exception, then the outerexceptblock is responsible for handling it.
Example
Explanation
- The outer
tryblock starts execution and printsouter try block. - The inner
tryblock starts and printsInner try block. print(10/0)raises aZeroDivisionError.- The inner
except ZeroDivisionErrorblock handles it and printsInner except block. - The inner
finallyblock runs and printsInner finally block. - Since the exception was already handled, the outer
exceptblock is skipped. - Finally, the outer
finallyblock runs and printsouter finally block.
Key Observations
- The inner
tryblock gets the first opportunity to execute. - The inner
exceptblock gets the first opportunity to handle exceptions raised by the innertry. - Once the exception is handled by the inner
except, it is not propagated to the outerexcept. - The inner
finallyblock always executes after the innertry. - The outer
finallyblock always executes after the outertry.
Control Flow in Nested try-except-finally
The control flow of nested try-except-finally blocks is represented as follows:
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
tryblock, then the correspondingfinallyblock will always be executed. - If the control does not enter the
tryblock, then the correspondingfinallyblock 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 QuestionsProgress: 0 / 7