Throw vs Throw e

Some people asked me the different between those two following code:

Code 1

try
{
    ...
}
catch (Exception ex)
{
   throw ex;
}

Code 2

try
{
    ...
}
catch (Exception ex)
{
    throw;
}

I don’t think there is ever a good reason to write catch (Exception e) { throw e; }. This loses the original stacktrace. When you use throw; the original stacktrace is preserved. This is good because it means that the cause of the error is easier to find.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.