What is the difference between throw and throw ex?
What is the difference between throw and throw ex?
320
19-Jun-2025
Ponu Maurya
23-Jun-2025In C#,
throwandthrow exboth raise exceptions, but they behave differently when it comes to preserving the original stack trace.throwvsthrow ex– Core Differencethrowthrow ex1.
throw– Rethrows the original exceptionWhen you use
throwalone inside acatchblock, it rethrows the original exception without losing the stack trace.2.
throw ex– Resets the stack traceWhen you use
throw ex, you create a new exception context, which resets the stack trace to the current line. This makes debugging harder.catchblock.Example:
Best Practice:
Use
throw ex;only if:Wrapping Example (valid use of
throw ex):