Bird
Raised Fist0

Consider this code:

hard🚀 Application Q9 of Q15
Python - Advanced Exception Handling
Consider this code:
try:
    print("Try block")
    x = 1 / 1
except ZeroDivisionError:
    print("Exception caught")
else:
    print("Else block")
finally:
    print("Finally block")

What is the output?
ATry block\nException caught\nFinally block
BTry block\nElse block\nFinally block
CTry block\nFinally block
DException caught\nElse block\nFinally block
Step-by-Step Solution
Solution:
  1. Step 1: Analyze try block

    Prints "Try block" and no exception occurs because 1/1 is valid.
  2. Step 2: Check except, else, finally

    Except skipped, else runs printing "Else block", finally always runs printing "Finally block".
  3. Final Answer:

    Try block\nElse block\nFinally block -> Option B
  4. Quick Check:

    No exception triggers else and finally = B [OK]
Quick Trick: Finally always runs; else runs only if no exception [OK]
Common Mistakes:
MISTAKES
  • Skipping finally output
  • Expecting except to run without error
  • Confusing else and finally order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes