Bird
0
0

Why is it generally recommended to use throw; instead of throw ex; when rethrowing exceptions in C#?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - Exception Handling
Why is it generally recommended to use throw; instead of throw ex; when rethrowing exceptions in C#?
ABecause <code>throw ex;</code> swallows the exception silently.
BBecause <code>throw ex;</code> is deprecated and causes compile errors.
CBecause <code>throw;</code> automatically logs the exception.
DBecause <code>throw;</code> preserves the original stack trace, aiding debugging.
Step-by-Step Solution
Solution:
  1. Step 1: Understand stack trace importance

    The original stack trace shows where the error started, which helps debugging.
  2. Step 2: Compare throw and throw ex

    throw; preserves this stack trace, while throw ex; resets it to current method, losing original info.
  3. Final Answer:

    Because throw; preserves the original stack trace, aiding debugging. -> Option D
  4. Quick Check:

    Stack trace preservation reason = A [OK]
Quick Trick: Use throw alone to keep debugging info intact [OK]
Common Mistakes:
MISTAKES
  • Thinking throw ex is deprecated
  • Assuming throw logs exceptions
  • Believing throw ex swallows exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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