Bird
Raised Fist0

Why does C# require that catch blocks be ordered from most specific to most general exceptions?

hard🧠 Conceptual Q10 of Q15
C Sharp (C#) - Exception Handling

Why does C# require that catch blocks be ordered from most specific to most general exceptions?

ATo optimize runtime performance by sorting exceptions.
BTo ensure finally block runs before catch blocks.
CBecause the compiler enforces this to avoid unreachable catch blocks.
DTo allow multiple catch blocks to run for one exception.
Step-by-Step Solution
Solution:
  1. Step 1: Understand compiler checks on catch order

    C# compiler checks catch blocks order to prevent unreachable code caused by general exceptions catching first.
  2. Step 2: Reason why unreachable catch blocks are disallowed

    If a general exception is caught first, specific catch blocks after it can never run, so compiler enforces order.
  3. Final Answer:

    Because the compiler enforces this to avoid unreachable catch blocks. -> Option C
  4. Quick Check:

    Compiler enforces catch order to prevent unreachable code [OK]
Quick Trick: Compiler disallows unreachable catch blocks by enforcing order [OK]
Common Mistakes:
MISTAKES
  • Thinking order is for performance only
  • Believing multiple catch blocks run for one exception
  • Confusing finally block order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes