70

Possible Duplicate:
difference between throw and throw new Exception()

What would be the point of just having

catch (Exception)
{
    throw;
}

What does this do?

Community
  • 1
  • 1
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • possible duplicate of [difference between throw and throw new Exception()](http://stackoverflow.com/q/2999298/) and [the difference between try/catch/throw and try/catch(e)/throw e](http://stackoverflow.com/q/1697216/). – Raymond Chen Jul 26 '12 at 22:29

5 Answers5

106

By itself, the throw keyword simply re-raises the exception caught by the catch statement above. This is handy if you want to do some rudimentary exception handling (perhaps a compensating action like rolling back a transaction) and then rethrow the exception to the calling method.

This method has one significant advantage over catching the exception in a variable and throwing that instance: It preserves the original call stack. If you catch (Exception ex) and then throw ex, your call stack will only start at that throw statement and you lose the method/line of the original error.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
14

Sometimes you might want to do something like this:

try
{
    // do some stuff that could cause SomeCustomException to happen, as 
    // well as other exceptions
}
catch (SomeCustomException)
{
    // this is here so we don't double wrap the exception, since
    // we know the exception is already SomeCustomException
    throw;
}
catch (Exception e)
{
    // we got some other exception, but we want all exceptions
    // thrown from this method to be SomeCustomException, so we wrap
    // it
    throw new SomeCustomException("An error occurred saving the widget", e);
}
dcp
  • 54,410
  • 22
  • 144
  • 164
  • 4
    +1 because the new throw passes the original exception in - very, very important (-: – Murph Nov 21 '11 at 15:01
  • @dcp Is there any difference if you removed the first catch block (the custom exception is still caught and re-thrown and other exceptions are still caught)? – Reid Moffat Jul 14 '22 at 19:54
  • 1
    @Reid Moffat - Yes there is a big difference. If you remove the first catch block and SomeCustomException is the exception thrown and caught, then you will double wrap the exception. In other words, you'll have SomeCustomException, with an inner exception of SomeCustomException, which is probably not what you want (that's what's meant by "double wrapping" the exception in the code comments above). – dcp Jul 14 '22 at 20:17
4

It rethrows the exact same error, you gain nothing by this.
Sometimes you can use the catch method to do some logging or something without interupting your exception like this:

catch (Exception) {
  myLogger.Log(LogLevels.Exception, "oh noes!")
  throw; 
}

I initially mistakingly thought this would unwind your stack, but this would only be if you would do the following:

catch (Exception err) {
  throw err; 
}
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • Are you sure you have the extra stack unwind, since using "throw" alone will just rethrow the original exception with the exact stacktrace as the original... right?! – veggerby Apr 28 '10 at 10:37
  • @boris: are you saying some code will be executed twice? – CJ7 Apr 28 '10 at 10:45
  • @Veggerby: You are right, I was confusing with the case where you say catch(Exception ex){throw ex}. In the case of the question's code, nothing happens really. @Craig: No nothing gets executed twice – Boris Callens Apr 28 '10 at 12:51
3

Only reason I can think of is if you want to put a breakpoint there during debugging.
It's also the default code being generated by some tools I think.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
2

Simply rethrow the current exception, and that exception will keep its "source" and the stack trace.

Rajesh
  • 6,269
  • 5
  • 28
  • 23