Bird
0
0

Why should the catch block for a subclass exception be placed before the superclass exception in Java?

hard📝 Conceptual Q10 of 15
Java - Exception Handling
Why should the catch block for a subclass exception be placed before the superclass exception in Java?
ABecause subclass exceptions are less important and should be last.
BBecause superclass catch blocks handle only unchecked exceptions.
CBecause Java matches exceptions top-down and subclass catch after superclass is unreachable.
DBecause catch blocks order does not matter in Java.
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception matching order

    Java checks catch blocks in order; first matching block runs.
  2. Step 2: Effect of catch order with inheritance

    If superclass catch is first, subclass catch is unreachable and causes compile error.
  3. Final Answer:

    Because Java matches exceptions top-down and subclass catch after superclass is unreachable. -> Option C
  4. Quick Check:

    Subclass catch before superclass to avoid unreachable code [OK]
Quick Trick: Subclass catch must come before superclass catch [OK]
Common Mistakes:
  • Placing superclass catch before subclass catch
  • Thinking catch order is irrelevant
  • Confusing checked and unchecked exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes