Bird
0
0

Which of the following is a correct way to rethrow an exception in a catch block without losing the original stack trace?

easy🧠 Conceptual Q2 of 15
C Sharp (C#) - Exception Handling
Which of the following is a correct way to rethrow an exception in a catch block without losing the original stack trace?
Athrow ex;
Bthrow;
Cthrow new Exception(ex.Message);
Dthrow ex.InnerException;
Step-by-Step Solution
Solution:
  1. Step 1: Identify rethrow syntax preserving stack trace

    Using throw; inside a catch block rethrows the current exception preserving the original stack trace.
  2. Step 2: Analyze other options

    throw ex; resets stack trace, throw new Exception(ex.Message); creates a new exception losing original info, and throw ex.InnerException; throws a different exception.
  3. Final Answer:

    throw; -> Option B
  4. Quick Check:

    Correct rethrow syntax = B [OK]
Quick Trick: Use throw alone to rethrow without losing info [OK]
Common Mistakes:
MISTAKES
  • Using throw ex instead of throw
  • Creating new exceptions instead of rethrowing
  • Throwing inner exceptions unintentionally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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