Bird
0
0

Identify the issue in this code snippet:

medium📝 Debug Q6 of 15
Python - Custom Exceptions
Identify the issue in this code snippet:
try:
    result = 10 / 0
except ZeroDivisionError as e:
    print('Zero division:', e)
except Exception as e:
    print('General error:', e)
AThe order of except blocks is incorrect; ZeroDivisionError should come before Exception
BZeroDivisionError cannot be caught with an except block
CThere is no issue; the code is correct
DException should be caught before ZeroDivisionError
Step-by-Step Solution
Solution:
  1. Step 1: Understand Exception Order

    More specific exceptions like ZeroDivisionError should be caught before more general exceptions like Exception.
  2. Step 2: Analyze the Code

    The code catches ZeroDivisionError first, then Exception. This is the correct order.
  3. Final Answer:

    There is no issue; the code is correct - The code is correct.
  4. Quick Check:

    Specific exceptions before general ones [OK]
Quick Trick: Catch specific exceptions before general Exception [OK]
Common Mistakes:
  • Placing general Exception before specific exceptions
  • Trying to catch ZeroDivisionError after Exception
  • Assuming ZeroDivisionError cannot be caught

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes