What is the difference between throw and throw ex in C# exception handling?
Understand the difference between throw and throw ex in C# exception handling, why stack traces matter, and how to rethrow correctly with examples.
Expected Interview Answer
Inside a catch block, 'throw;' re-throws the current exception while preserving its original stack trace, whereas 'throw ex;' throws the same object but resets the stack trace to the current line, hiding where the error actually originated.
The stack trace is the trail showing exactly which method and line raised the exception. When you write 'throw ex;', the runtime treats it as a brand-new throw point, so the trace starts at the catch block and the true origin is lost, making debugging far harder. 'throw;' rethrows the in-flight exception and keeps the full original trace. If you need to add context, wrap the exception in a new one and pass the original as the inner exception.
- 'throw;' preserves the original stack trace for debugging
- Avoids masking the true source of an error
- Keeps production logs accurate and actionable
- Lets you add context via inner exceptions when needed
- Prevents a common, silent code-review bug
AI Mentor Explanation
'throw;' is like a fielder relaying the ball with the umpire's full record of where the run-out started still intact, so the third umpire can trace every step. 'throw ex;' is like re-marking the play as if it began at the fielder's hands, erasing where the ball was first hit and leaving reviewers unable to see the real origin of the dismissal.
Step-by-Step Explanation
Step 1
Catch the exception
Enter a catch block where an exception object is in flight, e.g. 'catch (Exception ex)'.
Step 2
Decide whether to rethrow
If you cannot handle it here, you rethrow so an outer handler can deal with it.
Step 3
Use 'throw;' to preserve the trace
Write 'throw;' with no operand to rethrow the current exception keeping its original stack trace.
Step 4
Avoid 'throw ex;'
Writing 'throw ex;' resets the stack trace to this line, discarding the true origin.
Step 5
Add context safely if needed
Wrap it: 'throw new DataException("context", ex);' so the original is kept as InnerException.
What Interviewer Expects
- Clear statement that 'throw;' preserves the stack trace
- Understanding that 'throw ex;' resets the trace to the catch line
- Why the original stack trace matters for debugging
- Knowing to wrap in a new exception with InnerException for context
- Recognizing this as a real-world logging and debugging concern
Common Mistakes
- Believing 'throw' and 'throw ex' behave identically
- Using 'throw ex;' and losing the original stack trace in production
- Swallowing exceptions with an empty catch instead of rethrowing
- Not preserving the original exception as InnerException when wrapping
- Logging then rethrowing incorrectly and duplicating log noise
Best Answer (HR Friendly)
“In C#, 'throw' passes an error along while keeping the full record of where it started, but 'throw ex' throws the same error yet erases that history so it looks like the error began where you caught it. Almost always you want plain 'throw' so you can still find the real cause.”
Code Example
try
{
ProcessOrder();
}
catch (Exception ex)
{
_logger.LogError(ex, "Order failed");
throw; // GOOD: keeps original stack trace
// throw ex; // BAD: resets stack trace to this line
}
// Adding context while preserving the original:
catch (SqlException ex)
{
throw new DataException("Failed loading order", ex); // ex becomes InnerException
}Follow-up Questions
- How do you add context to an exception without losing the original trace?
- What is an InnerException and when do you use it?
- What happens if you swallow an exception in an empty catch?
- How does ExceptionDispatchInfo help rethrow across async boundaries?
- When should you catch a specific exception type versus the base Exception?
MCQ Practice
1. What does plain 'throw;' inside a catch block do to the stack trace?
'throw;' rethrows the in-flight exception and keeps its full original stack trace.
2. What is the main problem with 'throw ex;'?
'throw ex;' treats the throw as new, resetting the stack trace and hiding the original origin.
3. How should you add context while preserving the original error?
Throwing a new exception with the original passed as the inner exception keeps the full detail.
Flash Cards
What does 'throw;' preserve? — The original stack trace of the in-flight exception.
What does 'throw ex;' do wrong? — It resets the stack trace to the catch line, hiding the true origin.
How to add context safely? — Throw a new exception passing the original as InnerException.
Preferred rethrow in a catch? — Use bare 'throw;' unless you are wrapping with more context.
Continue Learning
Related Interview Questions
How do exception filters (catch ... when) differ from catch-and-rethrow, and why does that matter in production?
hard
What is pattern matching in C# and how has it evolved?
medium
What are indexers in C# and when should you use them?
medium
What is the difference between first-chance and second-chance exceptions in C#?
hard