I’ve got a new project. Every time you dealing with somebody else code it’s an adventure.
Here is what I found:
try
{
.....
}
catch (InvalidOperationException e) {
throw e;
}
catch (Exception e)
{
throw;
}
Anybody has an idea why?
PS
Thanks everybody.
It really helps.
Here are some good sources that you recommended:
Why catch and rethrow an exception in C#?
The real danger of this (other than being completely useless…) is that it modifies the call stack. Others have briefly mentioned it in comments, but it deserves to be called out specifically.
When you have
throw ex;, the previous call stack is blown away and replaced with the call stack at the point wherethrow ex;is called. You almost never want to do this. I will often catch an exception, log it, then rethrow the exception. When doing that, you want to just usethrow;. This will preserve the original stack trace.