Advertisement
Beginner C#, Exception Handling

What is the difference between throw and throw ex inside a catch?

Short answer

throw re-throws preserving the original stack trace; throw ex resets the stack trace to the current line, losing where the error really happened.

Save C# mock interview
Advertisement
AModel answer
throw re-throws preserving the original stack trace; throw ex resets the stack trace to the current line, losing where the error really happened.
catch (Exception ex) { Log(ex); throw; }      // good
catch (Exception ex) { throw ex; }             // loses stack trace
Real-world example
Preserving the original stack trace is essential for diagnosing production errors.