Code Explanation:
1️⃣ Outer try Block Starts
try:
Explanation
The outer try block begins.
Python will execute everything inside it.
If an exception occurs and is not handled inside → outer except runs.
2️⃣ First Statement
print("A")
Explanation
Prints:
A
No error yet, execution continues.
3️⃣ Inner try Block Starts
try:
Explanation
A nested try block begins inside the outer try.
It handles its own exceptions separately.
4️⃣ Raising an Exception
raise Exception
Explanation
An exception is raised manually.
Control immediately jumps to the inner except block.
5️⃣ Inner except Block
except:
Explanation
Catches the exception raised above.
Since it's a general except, it catches all exceptions.
6️⃣ Executing Inner except
print("B")
Explanation
Prints:
B
7️⃣ Inner finally Block
finally:
Explanation
finally always runs, whether exception occurred or not.
8️⃣ Executing Inner finally
print("C")
Explanation
Prints:
C
9️⃣ Outer except Block
except:
Explanation
This block runs only if an exception is not handled inside.
But here:
Inner except already handled the exception.
So outer except is NOT executed.
๐ค Final Output
A
B
C

0 Comments:
Post a Comment