'throw e;' vs. 'throw;'
Someone asked what the practical differences are between 'throw;' (no arguments) and 'throw object;' (in C# syntax).
Specifically, what's the difference between:
catch (Exception e)
{
throw;
}
and
catch (Exception e)
{
throw e;
}
This is mildly related to the 'catch' vs 'catch(Exception e)' vs. 'catch (SomeSpecificException e)'. I'll avoid talking about catch, and just focus on the rethrow.
What's the same:
Both throw an exception.
Both have the same debuggability problems associated with catch / rethrow.
They can be called in the same way. I used code like:
catch (Exception e) { if (...) throw; else throw e; }
as an academic way to demonstrate that they must have some similar properties. Never actually write code like that!
They can both be used in a naked catch block, although as difference #1 below mentions, 'throw e'; can't be used to rethrow the current exception.
catch
{
throw new MyWrapperException();
}
What's different:
- 'throw;' can only be used in the lexical scope of a catch block since it must gaurantee having a current exception. 'throw e' can be used anywhere.
- 'throw e' lets you provide a new exception object, such as a wrapper around the original exception.
- Only 'throw' can be used to rethrow the current exception within a naked catch block
- They both rethrow the current exception object, but “throw e;” resets parameters on it like the Exception.StackWalk property.
- They generate different IL opcodes. C#'s 'throw;' compiles to the 'rethrow' IL opcode, which doesn't take any parameters on the IL stack. 'throw e;' compiles to the 'throw' IL opcode, which takes 1 IL stack parameter: the exception to throw.
Check out the C# spec for more details