56

These days, I have been reading a lot the C++ F.A.Q and especially this page.

Reading through the section I discovered a "technique" that the author calls "exception dispatcher" that allows someone to group all his exception handling in one handy function:

void handleException()
{
  try {
    throw; // ?!
  }
  catch (MyException& e) {
    //...code to handle MyException...
  }
  catch (YourException& e) {
    //...code to handle YourException...
  }
}

void f()
{
  try {
    //...something that might throw...
  }
  catch (...) {
    handleException();
  }
}

What bothers me is the single throw; statement: if you consider the given example then sure, it is obvious what it does: it rethrows the exception first caught in f() and deals with it again.

But what if I call handleException() on its own, directly, without doing it from a catch() clause ? Is there any specified behavior ?

Additionally for bonus points, is there any other "weird" (probably not the good word) use of throw that you know of ?

Thank you.

ereOn
  • 53,676
  • 39
  • 161
  • 238
  • Did you read this on your linked page? http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.15 'In this example, the statement `throw;` means "re-throw the current exception."' – Fred Larson Mar 21 '11 at 14:05
  • @Fred Larson: Absolutely. But in the case where there is no "current" exception, what happens ? – ereOn Mar 21 '11 at 14:06
  • I found the answer, but @dalle beat me to it. – Fred Larson Mar 21 '11 at 14:10

3 Answers3

55

If you do a throw; on its own, and there isn't a current exception for it to rethrow, then the program ends abruptly. (More specifically, terminate() is called.)

Note that throw; is the only safe way to re-throw the current exception - it's not equivalent to

catch (exception const & e) { throw e; }

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
  • Thanks for your answer. This might be part of another question but is surely related: could you explain the difference between calling "throw;" and "throw e;" in your last statement ? I wasn't aware of the difference. – ereOn Mar 21 '11 at 14:10
  • 2
    @ereOn: I believe at least one difference is that `throw;` will preserve the original exception type, while `throw e;` may slice. – Fred Larson Mar 21 '11 at 14:12
  • @Fred Larson: I thought slice may happen only when catching by value. But you might be right (I'm not sure at all). Is there anyone to confirm that ? – ereOn Mar 21 '11 at 14:16
  • 12
    Yes - `throw e;` throws a new exception with the static type of e, not the dynamic type - so it can slice if e is polymorphic. – Alan Stokes Mar 21 '11 at 14:16
  • Additionally, the second example would only rethrow the current exception if it derived from `std::exception`, correct? – NHDaly Mar 13 '13 at 04:51
  • 1
    @NHDaly Yes. Any other exception would not be caught (and so wouldn't need rethrowing anyway). But nice people don't throw things that don't derive from `std::exception`. – Alan Stokes Mar 24 '13 at 15:31
41

Yes, it specified behavior, it will call terminate;

15.1, para 8: If no exception is presently being handled, executing a throw expression with no operand calls terminate() (15.5.1).

dalle
  • 18,057
  • 5
  • 57
  • 81
  • 3
    I wish I could accept this answer as well: I accepted Alan Stokes's answer because he currently has a lower reputation than you. He also provided a detailed and accurate answer. Upvoted this one for fairness tought. Thank you very much. – ereOn Mar 21 '11 at 14:19
8

That's so-called exception handler. It rethrows the "current exception" if any. If there's no exception currently being handled terminate() will be called.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979