Code Explanation:
๐น 1. Function Definition
def func():
✅ Explanation:
A function named func is created.
It contains:
try block
except block
finally block
๐น 2. try Block
try:
print(10 / 0)
✅ Explanation:
Python tries to execute:
10 / 0
⚠️ Problem:
Division by zero is not allowed.
So Python raises:
ZeroDivisionError
๐น 3. Exception Occurs
ZeroDivisionError
✅ Explanation:
Since an error occurs inside try,
Python immediately stops the remaining try code
It searches for a matching except
๐น 4. except Block
except ZeroDivisionError:
print("ERROR")
✅ Explanation:
This block catches only:
ZeroDivisionError
✔️ So it runs:
print("ERROR")
Output:
ERROR
๐น 5. finally Block
finally:
print("DONE")
✅ Explanation:
finally always executes:
Error occurred ✅
No error ✅
Return statement ✅
✔️ So it prints:
DONE
๐น 6. Function Call
func()
✅ Execution Flow:
try → error occurs
↓
except runs
↓
finally runs
๐ฏ Final Output
ERROR
DONE

0 Comments:
Post a Comment