The difference between throw; and throw ex;

Most people don't even realize you can just say "throw;" within a catch block to re-throw the original exception. But my good friend Matt has pointed out an important difference that goes all too unnoticed these days. So important, it could mean the difference in a debug session between "ah ha!" and "what the?"

Anyway, if you don't know what the difference is between this:

try {
    // do something bad
}
catch(Exception ex) {
    // log the error somewhere
    LogException(ex);
    // rethrow it
    throw ex;
}

and this:

try {
    // do something bad
}
catch(Exception ex) {
    // log the error somewhere
    LogException(ex);
    // rethrow it
    throw;
}

Then you should definitely read about it here. Now that being said, there's always the debate about whether one should ever be catching the general Exception (I personally believe there are definitely times where it is appropriate), but whatever side of that fence you are on, it's still important to know how to properly rethrow an exception.

1 Comment

Comments have been disabled for this content.