Thursday, August 17, 2006 11:05 AM InfinitiesLoop

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.

Filed under:

Comments

# Link Listing - August 20, 2006

Sunday, August 20, 2006 11:29 PM by Christopher Steen

AnkhSVN: Using Subversion within Visual Studio [Via: Scott Hanselman ] Another Webcast - on Contract-First...

# re: The difference between throw; and throw ex;

Thursday, April 26, 2007 1:37 PM by Jon

So what is the difference between the two?

Leave a Comment

(required) 
(required) 
(optional)
(required)