Bird
Raised Fist0

What is the issue with this exception handling code?

medium📝 Debug Q7 of Q15
Python - Exception Handling Fundamentals

What is the issue with this exception handling code?

try:
    result = 10 / 0
except:
    print('General exception caught')
except ZeroDivisionError:
    print('ZeroDivisionError caught')
AThe general <code>except</code> block should come after the specific <code>ZeroDivisionError</code> block.
BThe code is correct; order of except blocks does not matter.
CThe <code>except ZeroDivisionError</code> block should be removed.
DThe <code>try</code> block should be inside the <code>except</code> block.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Except Block Order

    Python checks except blocks in order; specific exceptions must come before general ones.
  2. Step 2: Analyze Code

    The general except: block is first, so it catches all exceptions, making the specific block unreachable.
  3. Step 3: Correct Order

    Place the except ZeroDivisionError: block before the general except: block.
  4. Final Answer:

    The general except block should come after the specific ZeroDivisionError block. is correct.
  5. Quick Check:

    Specific except blocks must precede general ones [OK]
Quick Trick: Order except blocks from specific to general [OK]
Common Mistakes:
MISTAKES
  • Placing general except before specific ones
  • Assuming except order does not affect handling
  • Using bare except before specific exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes