views:

302

answers:

6

I am curious about the rationale behind noexcept in the C++0x FCD. throw(X) was depreciated, but noexcept seems to do the same thing. Is there a reason that noexcept isn't checked at compile time? It seems that it would be better if these functions were checked statically that they only called throwing functions within a try block.

+5  A: 

If I remember throw has been deprecated because there is no way to specify all the exceptions a template function can throw. Even for non-template functions you will need the throw clause because you have added some traces.

On the other hand the compiler can optimize code that doesn't throw exceptions. See "The Debate on noexcept, Part I" (along with parts II and III) for a detailed discussion. The main point seems to be:

The vast experience of the last two decades shows that in practice, only two forms of exceptions specifications are useful:

  • The lack of an overt exception specification, which designates a function that can throw any type of exception:

    int func(); //might throw any exception
    
  • A function that never throws. Such a function can be indicated by a throw() specification:

    int add(int, int) nothrow; //never throws
    
Vicente Botet Escriba
+6  A: 

Note that noexcept checks for an exception thrown by a failing dynamic_cast and typeid applied to a null pointer, which can only be done at runtime. Other tests can indeed be done at compile time.

outis
However, it is possible to simply forbid `dynamic_cast` or `typeid` outside a `try` block...
rlbond
Except that it might not be suitable to catch the exception at that point. The exception could be re-thrown, but that's adding unnecessary verbosity and overhead.
outis
A: 

Consider a function

void fn() noexcept
{
   foo();
   bar();
}

Can you statically check if its correct? You would have to know whether foo or bar are going to throw exceptions. You could force all functions calls to be inside a try{} block, something like this

void fun() noexcept
{
    try
    {
         foo();
         bar();
    }
    catch(ExceptionType &)
    {
    }
}

But that is wrong. We have no way of knowing that foo and bar will only throw that type of exception. In order to make sure we catch anything we'd need to use "...". If you catch anything, what are you going to do with any errors you catch? If an unexpected error arises here the only thing to do is abort the program. But that is essentially what the runtime check provided by default will do.

In short, providing enough details to prove that a given function will never throw the incorrect exceptions would produce verbose code in cases where the compiler can't be sure whether or not the function will throw the wrong type. The usefulness of that static proof probably isn't worth the effort.

Winston Ewert
I disagree. If we have a complete prototype for foo and bar then we will know if they have exception specifications. The only way this code will compile (even if foo and bar are in dynamicly loaded libraries) is if we have proper prototypes for them at compile time.
SoapBox
@SoapBox: But the only non-deprecated specifications are "might throw" and "doesn't throw." Whether or not `fun` throws when `foo` and `bar` have the "might throw" specifications is based on *what* they might throw. If they throw `ExceptionType` objects, `fun` is fine. If they throw `OtherExceptionType` objects, `fun` is not fine. The prototypes are insufficient to determine this.
Dennis Zickefoose
A: 

I think you'll want to take a look at this article covering the last-minute addition of noexcept.

Ken Bloom
Note that that's the same article linked to by Vicente.
outis
@outis: I swear I googled the answer *all by myself* :)
Ken Bloom
We all did, Ken, we all did. Well, almost all of us.
outis
+3  A: 

There is some overlap in the functionality of noexcept and throw(), but they're really coming from opposite directions.

throw() was about correctness, and nothing else: a way to specify the behavior if an unexpected exception was thrown.

The primary purpose of noexcept is optimization: It allows/encourages the compiler to optimize around the assumption that no exceptions will be thrown.

But yes, in practice, they're not far from being two different names for the same thing. The motivations behind them are different though.

jalf
A: 

As other answers have stated, statements such as dynamic_casts can possibly throw but can only be checked at runtime, so the compiler can't tell for certain at compile time.

This means at compile time the compiler can either let them go (ie. don't compile-time check), warn, or reject outright (which wouldn't be useful). That leaves warning as the only reasonable thing for the compiler to do.

But that's still not really useful - suppose you have a dynamic_cast which for whatever reason you know will never fail and throw an exception because your program is written so. The compiler probably doesn't know that and throws a warning, which becomes noise, which probably just gets disabled by the programmer for being useless, negating the point of the warning.

A similar issue is if you have a function which is not specified with noexcept (ie. can throw exceptions) which you want to call from many functions, some noexcept, some not. You know the function will never throw in the circumstances called by the noexcept functions, but again the compiler doesn't: more useless warnings.

So there's no useful way for the compiler to enforce this at compile time. This is more in the realm of static analysis, which tend to be more picky and throw warnings for this kind of thing.

AshleysBrain