I saw some code the other day in one of our projects that uses a try catch and re-throws the caught exception like this:
try
{
exceptionProneCode();
}
catch(Exception ex)
{
throw ex;
}
Nothing else was done with the exception in the catch block so I'm not even sure why it's re-thrown. I can't see any benefit to actually throwing the same exception again and doing nothing with the exception.
If you re-throw an exception that's caught in the catch block, how does C# handle this? Does it get stuck in an infinite throw/catch loop? Or does it eventually leave the try catch?
finally
with thecatch
? Maybe someone accidentally included thecatch
out of habit? (one can hope) One possible benefit would be step-through-debugging could break-point the catch (not advocating! I'd probably wrap with extra data and re-throw the new chain.)finally
included with thecatch
. I just didn't fully understand why one would re-throw the caught exception. I'm getting a better idea about that now.