What is the difference between throw and throw ex?
What is the difference between throw and throw ex?
Student
The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
In 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):