What is the point of `void func() throw(type)`?

2020-02-11 08:20发布

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; }

6条回答
不美不萌又怎样
2楼-- · 2020-02-11 08:29

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.

查看更多
干净又极端
3楼-- · 2020-02-11 08:38

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 */
    }
}
查看更多
Summer. ? 凉城
4楼-- · 2020-02-11 08:39

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.

查看更多
虎瘦雄心在
5楼-- · 2020-02-11 08:41

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.

查看更多
一夜七次
6楼-- · 2020-02-11 08:44

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().

查看更多
放我归山
7楼-- · 2020-02-11 08:47

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

查看更多
登录 后发表回答