21

I just stumbled this code:

void somefunction()
{
   throw;
}

and I wonder: what does it mean?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
  • 3
    This question is not related to Visual Studio or any kind of "visual" c++. Please, don't tag generic C++ questions with concrete framework tags. – P Shved Apr 22 '10 at 16:05
  • @Pavel Shved: The question could be related to VC++ if there was some sort of language extension applicable. In that case, it would be desirable to answer it on that basis, while also pointing out the answer according to the Standard. – David Thornley Apr 22 '10 at 16:38
  • @David, @Tobias, I think it's better not to tag the question as VC-related if you're not sure. The community will retag it appropriately if it happens to be a language extension. – P Shved Apr 22 '10 at 16:49
  • @Pavel: I think I'd rather see it tagged as VC++-related if the questioner isn't sure, myself. It's certainly debateable. In any case, Tobias, please make sure you specify the platform when it matters, although exactly where is a matter of disagreement right now. – David Thornley Apr 22 '10 at 17:02
  • Possible duplicate of [What happens if a throw; statement is executed outside of catch block?](http://stackoverflow.com/questions/981400/what-happens-if-a-throw-statement-is-executed-outside-of-catch-block) – anton_rh Jun 01 '16 at 06:58

4 Answers4

32

The intent is probably that somefunction() is only ever called from inside some catch block. In that case, there would be an exception active when the throw; is executed, in which case the current exception is re-thrown, to be caught by the next outer handler that can handle that exception type.

If throw; is executed when an exception is not active, it calls terminate() (N4810, §[expr.throw]/4).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
9

It re-throws the currently active exception. It would only make sense to call it (possibly indirectly) from a catch-block. This:

#include <iostream>
using namespace std;

void f() {
    throw;
}

int main() {
    try {
        try {
            throw "foo";
        }
        catch( ... ) {
            f();
        }
    }
    catch( const char * s ) {
        cout << s << endl;
    }
}

prints "foo".

5

For throw the concept of being "outside" or "inside" catch block is defined in run-time terms, not in compile-time terms as you seem to assume. So, if during run-time that throw is executed in run-time context of a catch block, then throw works as expected. Otherwise, terminate() is called.

In fact, if you take a closer look at how C++ exceptions are defined in the language specification, a lot of things about them are defined in run-time terms. Sometimes it even appears to be un-C++-like.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
3

People have already explained what it means but it's potentially useful to know why you might see it. It's a useful way to construct a 'generic' exception handler that deals with exceptions based on their type so as to reduce the amount of duplicated code.

So, if we take Neil's example and expand on what f() might be doing we might end up with an implementation which does something like my LogKnownException() function that I proposed in this answer.

If you are working in an team that likes to log all manner of exceptions all over the place then rather than having a huge collection of catch blocks at all of these places (or even worse a macro) you can have a simple catch block that looks like this

catch(...)
{
   LogKnownException();
}  

Though I expect I'd change my previous example of LogKnownException() to one that simply allowed exceptions that it didn't want to log to propagate out and continue on in an unhandled fashion.

I'm not suggesting that this is necessarily a good thing to do, just pointing out that this is where you're likely to see the construct used.

Community
  • 1
  • 1
Len Holgate
  • 21,282
  • 4
  • 45
  • 92