Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Advanced Exception Handling
What will be the output of this code?
try:
    print('Start')
    x = 1 / 0
except ZeroDivisionError:
    print('Error caught')
finally:
    print('Always runs')
AStart\nAlways runs
BStart\nError caught
CError caught\nAlways runs
DStart\nError caught\nAlways runs
Step-by-Step Solution
Solution:
  1. Step 1: Trace the try block

    The code prints 'Start' then tries to divide by zero, causing a ZeroDivisionError.
  2. Step 2: Handle the exception and finally block

    The except block catches the error and prints 'Error caught'. Then the finally block runs and prints 'Always runs'.
  3. Final Answer:

    Start\nError caught\nAlways runs -> Option D
  4. Quick Check:

    try prints + except prints + finally prints = A [OK]
Quick Trick: finally always prints last, even after except. [OK]
Common Mistakes:
MISTAKES
  • Forgetting finally runs
  • Assuming code stops after error
  • Missing the initial print before error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes