Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
Python - Exception Handling Fundamentals
Identify the error in this code snippet:
try:
    x = int('hello')
except ZeroDivisionError:
    print('Cannot divide by zero')
AWrong exception caught, should catch ValueError
BSyntaxError due to missing colon
CNo error, code runs fine
DZeroDivisionError will be raised
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    int('hello') raises ValueError because 'hello' cannot convert to int.
  2. Step 2: Check except block

    Except block catches ZeroDivisionError, which does not handle ValueError, so error is uncaught.
  3. Final Answer:

    Wrong exception caught, should catch ValueError -> Option A
  4. Quick Check:

    Exception type mismatch = catch correct exception [OK]
Quick Trick: Catch the exact exception your code may raise [OK]
Common Mistakes:
  • Catching wrong exception type
  • Assuming code runs without error
  • Confusing syntax errors with exception handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes