Bird
0
0

What is the main difference between throw; and throw ex; inside a catch block in C#?

easy🧠 Conceptual Q1 of 15
C Sharp (C#) - Exception Handling
What is the main difference between throw; and throw ex; inside a catch block in C#?
A<code>throw;</code> preserves the original stack trace, <code>throw ex;</code> resets it.
B<code>throw ex;</code> preserves the original stack trace, <code>throw;</code> resets it.
CBoth reset the stack trace to the current method.
DBoth preserve the original stack trace.
Step-by-Step Solution
Solution:
  1. Step 1: Understand throw; behavior

    throw; rethrows the caught exception without changing the stack trace, preserving where the exception originally occurred.
  2. Step 2: Understand throw ex; behavior

    throw ex; throws the exception object again but resets the stack trace to the current method, losing original error location.
  3. Final Answer:

    throw; preserves the original stack trace, throw ex; resets it. -> Option A
  4. Quick Check:

    Stack trace preservation = B [OK]
Quick Trick: Use plain throw to keep original error location [OK]
Common Mistakes:
MISTAKES
  • Confusing throw and throw ex stack trace behavior
  • Assuming both preserve stack trace
  • Thinking throw ex is safer than throw

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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