3

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; }
  • One of the things in C++ that I learned but never used nor I will :) – Khaled Alshaya Oct 19 '09 at 15:48
  • 3
    "don't bother with exception specifications. Even experts don't bother. They don't do what most people think, and you almost always don't want what they actually do." Andrei Alexandrescu & Herb Sutter – KeatsPeeks Oct 19 '09 at 15:51
  • As much as i respect Sutter and Alexandrescu. But that's a bad advice. Just because they are bad doesn't mean you shouldn't bother learning them. It will help you explain others why they are bad and make you understand why you actually should avoid them. Not just applying a dogmatic rule is a good thing. – Johannes Schaub - litb Oct 19 '09 at 15:56
  • 1
    http://www.gotw.ca/publications/mill22.htm – sellibitze Oct 19 '09 at 16:08
  • good then :) i couldn't imagine they just recommend people not to bother with them. In context, it probably reads a bit different :) cheers – Johannes Schaub - litb Oct 19 '09 at 16:13
  • @TheSam, do you have a link to that article, or is it out of their book? – Johannes Schaub - litb Oct 19 '09 at 16:16
  • whoa, litb, long time no see. –  Oct 19 '09 at 17:03
  • I found it here: http://herbsutter.wordpress.com/2007/01/24/questions-about-exception-specifications/ . And yeah, it goes on after that explaining why people shouldn't use them. So i guess i misunderstood "don't bother" as meaning "don't read about them". – Johannes Schaub - litb Oct 19 '09 at 17:07

6 Answers6

18

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
  • 56,304
  • 9
  • 91
  • 158
  • Best answer. Learners HAVE TO KNOW they should not bother with them. – KeatsPeeks Oct 19 '09 at 15:52
  • 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. –  Oct 19 '09 at 15:55
12

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
  • 97,747
  • 36
  • 197
  • 212
2

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
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 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 Oct 19 '09 at 16:11
0

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
  • 476,176
  • 80
  • 629
  • 1,111
0

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
  • 108,024
  • 16
  • 131
  • 187
0

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
  • 257,169
  • 86
  • 333
  • 562