Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - Exception Handling
Identify the error in this code snippet:
try {
  // some code
} catch (Exception ex) {
  throw ex;
}
ATry block must have a finally block.
BUsing <code>throw ex;</code> resets the stack trace, losing original error info.
CCannot catch Exception type directly.
DMissing semicolon after <code>throw ex</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Understand throw ex; effect

    Using throw ex; throws the exception but resets the stack trace, losing original error location.
  2. Step 2: Identify correct rethrow method

    To preserve stack trace, use throw; without specifying the exception variable.
  3. Final Answer:

    Using throw ex; resets the stack trace, losing original error info. -> Option B
  4. Quick Check:

    Throwing exception variable resets stack trace = D [OK]
Quick Trick: Use 'throw;' not 'throw ex;' to keep original stack trace [OK]
Common Mistakes:
MISTAKES
  • Thinking 'throw ex;' preserves stack trace
  • Believing finally block is mandatory
  • Assuming Exception cannot be caught directly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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