0

Sometimes in code I see throwing exceptions where the throw keyword is used without an expression next to it:

throw;

What does it mean and when should I use it?

template boy
  • 10,230
  • 8
  • 61
  • 97
  • and [another one...](http://stackoverflow.com/questions/1833982/in-c-is-there-a-difference-between-throw-and-throw-ex) (Fred's comment in this one is priceless, btw). – WhozCraig Aug 27 '13 at 14:12
  • @WhozCraig funny. I didn't remember it, despite me having trolled that ancient acient-question-trolling comment with my own comment little over a year ago :/ – sehe Aug 27 '13 at 14:17

5 Answers5

5

An empty throw rethrows the exception.

It can appear only in a catch or in a function called from a catch.

If an empty throw is encountered when a handler is not active, terminate will be called.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

It rethrows the exception of the currently active catch block.

try {
  foo();
} catch (...) {
  fix_some_local_thing();
  throw; // keep searching for an error handler
}

If there is no currently active catch block, it will call std::terminate.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • What is an "active catch block"? – sehe Aug 27 '13 at 14:14
  • The most recent one entered by exception handling. (You can have a try..catch inside a catch block, and thus have nested exception handling.) – Sebastian Redl Aug 27 '13 at 14:15
  • This terminology is new for me. Also, I have never considered 'catch' blocks active. 'try/catch' blocks, maybe. Then, still, 'active' seems like a strange notion that isn't really consistent with the C++ abstract machine model. I'd opt for 'exception handlers registered in the current callstack', but I'd be interested if you have a source for your/better terminology. – sehe Aug 27 '13 at 14:24
  • I believe that "active" is the term used by Standard- at least, I've seen it many times before. – Puppy Aug 27 '13 at 14:25
  • "current exception object", or "currently handled exception". – R. Martinho Fernandes Aug 27 '13 at 14:25
  • R.MartinhoFernandes That's about the exception, not the handler. @DeadMG Thanks! – sehe Aug 27 '13 at 14:25
  • I used whatever word came to mind, not any standard terminology. – Sebastian Redl Aug 27 '13 at 14:26
  • @sehe the handler doesn't matter. The standard describes the functionality in terms of the exception. Example: http://coliru.stacked-crooked.com/view?id=fbd004ed6b18aacfdbd1f84a235728e9-00cd033b7ad7a487b1892674bc8212f7 – R. Martinho Fernandes Aug 27 '13 at 14:26
1

It's a rethrow of the current exception.

This doesn't alter the current exception, effectively just resuming the stack-unwinding after doing the actions in your catch{} block.

Quoting the standard: § 15.1 ad 8 [except.throw]

A throw-expression with no operand rethrows the currently handled exception (15.3). The exception is reactivated with the existing temporary; no new temporary exception object is created. The exception is no longer considered to be caught; therefore, the value of std::uncaught_exception() will again be true.

Example: code that must be executed because of an exception yet cannot completely handle the exception can be written like this:

try {
// ...
} catch (...) { // catch all exceptions
// respond (partially) to exception
throw; // pass the exception to some
// other handler
}
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
1

In a function declaration, it means that the function won't throw any exception.

In a catch block, it re-throws the caught exception, e.g.

try {
   throw "exception";
catch ( ... ) {
   std::cout << "caught exception";
   throw; // Will rethrow the const char* exception
}
king_nak
  • 11,313
  • 33
  • 58
0

It's used inside a catch block to rethrow the current exception.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54