Code Explanation:
1️⃣ Starting the try Block
try:
Explanation
Code inside try is executed first.
If any error occurs → control moves to except.
2️⃣ First Statement in try
print(1)
Explanation
Prints:
1
No error yet, so execution continues.
3️⃣ Raising an Exception
raise Exception
Explanation
Manually raises an Exception.
Immediately stops normal execution of try.
Control jumps to the except block.
bexcept:
Explanation
Catches the raised exception.
Since it's a generic except, it catches any error.
5️⃣ Executing except Block
print(2)
Explanation
Prints:
2
6️⃣ Entering the finally Block
finally:
Explanation
finally block always executes, no matter what:
whether exception occurs or not
whether return happens or not
7️⃣ Executing finally Block
print(3)
Explanation
Prints:
3
๐ค Final Output
1
2
3

0 Comments:
Post a Comment