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.

Advertsing

125X125_06

TagCloud

MonthList

CommentList