views:

186

answers:

6

I know this is a valid c++ program. What is the point of the throw in the function declarement? AFAIK it does nothing and isnt used for anything.

#include <exception>
void func() throw(std::exception) { }
int main() { return 0; }
A: 

This is an exception specification. It says that the only exception that func() can throw is std::exception (or a derivative thereof). Attempting to throw any other exception will give std::unexpected instead.

Jerry Coffin
+10  A: 

That is an exception specification, and it is almost certainly a bad idea.

It states that func may throw a std::exception, and any other exception that func emits will result in a call to unexpected().

Dominic Rodger
A: 

Exception specification. The type(s) following the throw keyword specifies exactly what all, if any, exceptions the function can throw. See 15.4 of the draft.

Note: A function with no exception-specification allows all exceptions. A function with an empty exception-specification, throw(), does not allow any exceptions.

dirkgently
+2  A: 

This is a C++ exception specification. It declares that the particular function will potentially throw a std::exception type.

In general though exception specifications in C++ are considered a feature to avoid. It's an odd feature in that it's behavior is declared at compile time but only checked at runtime (very different from say Java's version).

Here is a good article which breaks down the feature

JaredPar
The difference with Java is what got me confused when I started learning C++. I used them all over the place because they felt familiar, and then later went through and removed them all after reading GOTW.
Dominic Rodger
+16  A: 

It specifies that any std::exception can be thrown from func(), and nothing else. If something else is thrown, it will call an unexpected() function which by default calls terminate().

What this means is that throwing something else will almost certainly terminate the program, in the same manner as an uncaught exception, but the implementation will have to enforce this. This is normally much the same as putting a try{...}catch(){...} block around func(), which can inhibit performance.

Usually, exception specifications aren't worth it, according to the Guru of the Week column about it. The Boost guidelines say that there might be a slight benefit with a blank throws() for a non-inline function, and there are disadvantages.

David Thornley
Best answer. Learners HAVE TO KNOW they should not bother with them.
Samuel_xL
I really like this answer and had a hard time choosing. Dominic wins because i usually like answers that are 1-2 sentence with links.
acidzombie24
A: 

Basically this:

void func() throw(std::exception,B) {  /* Do Stuff */}

Is just shorthand fro this:

void func()
{
    try
    {
        /* Do Stuff */ 
    }
    catch(std::exception const& e)
    {
        throw;
    }
    catch(B const& e)
    {
        throw;
    }
    catch(...)
    {
        unexpected();  // This calls terminate
                       // i.e. It never returns.
    }
}

Calling terminate() is rarely what you want, as the stack is not unwound and thus all your efforts in RAII is wasted. The only exception to the rule is declaring an empty throw list and this is mainly for documentation purposes to show that you are supporting the no-throw exception gurantee (you should still manually catch all exceptions in this situation).

Some important (imho) places that should be no-throw are destructors and swap() methods. Destructors are rarely explicitly marked no-throw but swap() are quite often marked no-throw.

void myNoThrowFunc() throws()  // No-Throw (Mainlly for doc purposes).
{
    try
    {
        /* Do Stuff */
    }
    catch(...)  // Make sure it does not throw.
    {
        /* Log and/or do cleanup */
    }
}
Martin York