Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Custom Exceptions
What will be the output of this code?
try:
    x = 1 / 0
except ArithmeticError:
    print('ArithmeticError caught')
except ZeroDivisionError:
    print('ZeroDivisionError caught')
AArithmeticError caught
BZeroDivisionError caught
CNo output, program crashes
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception hierarchy for ZeroDivisionError

    ZeroDivisionError is a subclass of ArithmeticError.
  2. Step 2: Check which except block matches first

    Since ArithmeticError comes before ZeroDivisionError, the first except block catches the exception.
  3. Final Answer:

    ArithmeticError caught -> Option A
  4. Quick Check:

    Parent exception catches before child [OK]
Quick Trick: Parent exceptions catch before child exceptions in order [OK]
Common Mistakes:
  • Expecting ZeroDivisionError block to run first
  • Thinking exception order does not matter
  • Assuming program crashes without handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes