Code Explanation:
๐น 1. Function Definition
def func():
✅ Explanation:
A function named func() is created.
The code inside the function will run only when func() is called.
๐น 2. Entering try Block
try:
✅ Explanation:
Python starts executing the code inside the try block.
If an exception occurs, control moves to the matching except block.
๐น 3. First Print Statement
print("A")
✅ Explanation:
Python prints:
A
Current Output:
A
๐น 4. Division by Zero
1 / 0
✅ Explanation:
Python tries to calculate:
1 ÷ 0
❌ Problem:
Division by zero is not allowed.
Python raises:
ZeroDivisionError
๐น 5. Exception Occurs
Because an exception happened:
1 / 0
Python immediately stops executing the remaining code inside try.
Control jumps to:
except ZeroDivisionError:
๐น 6. Matching except Block
except ZeroDivisionError:
✅ Explanation:
The raised exception is:
ZeroDivisionError
and the except block is specifically handling:
ZeroDivisionError
So this block executes.
๐น 7. Print Inside except
print("B")
✅ Explanation:
Python prints:
B
Current Output:
A
B
๐น 8. Entering finally
finally:
✅ Explanation:
finally always executes whether:
Exception occurs ✅
No exception occurs ✅
Return statement executes ✅
๐น 9. Print Inside finally
print("C")
✅ Explanation:
Python prints:
C
Current Output:
A
B
C
๐น 10. Function Call
func()
✅ Explanation:
Calls the function.
Entire execution described above takes place.
๐ฏ Final Output
A
B
C

0 Comments:
Post a Comment