135

In C++, you can specify that a function may or may not throw an exception by using an exception specifier. For example:

void foo() throw(); // guaranteed not to throw an exception
void bar() throw(int); // may throw an exception of type int
void baz() throw(...); // may throw an exception of some unspecified type

I'm doubtful about actually using them because of the following:

  1. The compiler doesn't really enforce exception specifiers in any rigorous way, so the benefits are not great. Ideally, you would like to get a compile error.
  2. If a function violates an exception specifier, I think the standard behaviour is to terminate the program.
  3. In VS.Net, it treats throw(X) as throw(...), so adherence to the standard is not strong.

Do you think exception specifiers should be used?
Please answer with "yes" or "no" and provide some reasons to justify your answer.

iammilind
  • 68,093
  • 33
  • 169
  • 336
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • 7
    "throw(...)" is not standard C++. I believe it is an extension in some compilers and generally has the same meaning as no exception specification. – Richard Corden Sep 18 '08 at 10:38

14 Answers14

106

No.

Here are several examples why:

  1. Template code is impossible to write with exception specifications,

    template<class T>
    void f( T k )
    {
         T x( k );
         x.x();
    }
    

    The copies might throw, the parameter passing might throw, and x() might throw some unknown exception.

  2. Exception-specifications tend to prohibit extensibility.

    virtual void open() throw( FileNotFound );
    

    might evolve into

    virtual void open() throw( FileNotFound, SocketNotReady, InterprocessObjectNotImplemented, HardwareUnresponsive );
    

    You could really write that as

    throw( ... )
    

    The first is not extensible, the second is overambitious and the third is really what you mean, when you write virtual functions.

  3. Legacy code

    When you write code which relies on another library, you don't really know what it might do when something goes horribly wrong.

    int lib_f();
    
    void g() throw( k_too_small_exception )
    { 
       int k = lib_f();
       if( k < 0 ) throw k_too_small_exception();
    }
    

    g will terminate, when lib_f() throws. This is (in most cases) not what you really want. std::terminate() should never be called. It is always better to let the application crash with an unhandled exception, from which you can retrieve a stack-trace, than to silently/violently die.

  4. Write code that returns common errors and throws on exceptional occasions.

    Error e = open( "bla.txt" );
    if( e == FileNotFound )
        MessageUser( "File bla.txt not found" );
    if( e == AccessDenied )
        MessageUser( "Failed to open bla.txt, because we don't have read rights ..." );
    if( e != Success )
        MessageUser( "Failed due to some other error, error code = " + itoa( e ) );
    
    try
    {
       std::vector<TObj> k( 1000 );
       // ...
    }
    catch( const bad_alloc& b )
    { 
       MessageUser( "out of memory, exiting process" );
       throw;
    }
    

Nevertheless, when your library just throws your own exceptions, you can use exception specifications to state your intent.

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Christopher
  • 8,912
  • 3
  • 33
  • 38
  • 1
    in 3, it would technically be std::unexpected not std::terminate. But when this function is called, the default invokes abort(). This generates a core dump. How is this worse than an unhandled exception? (which does basically the same thing) – Greg Rogers Sep 18 '08 at 13:48
  • 6
    @Greg Rogers: An uncatched exception still does stack unwinding. This means destructors will be called. And in those destructors, a lot can be done, like: Resources correctly freed, logs correctly written, other processes will be told the current process is crashing, etc.. To summarize, it's RAII. – paercebal Dec 05 '08 at 23:53
  • You left out: This effectively wraps everything in a `try {...} catch () { } catch (...) { unexpected(); ]` construct, whether or not you want a try block there. – David Thornley Mar 01 '10 at 15:29
  • 5
    @paercebal That is incorrect. It is implementation defined whether or not destructors are run for uncaught exceptions. Most environments don't unwind the stack/run destructors if the exception isn't caught. If you want to portably ensure your destructors run even when an exception is thrown and not handled (this is of dubious value), you need to write your code like `try { <<...code...>> } catch(...) /* stack guaranteed to be unwound here and dtors run */ { throw; /* pass it on to the runtime */ }` – Logan Capaldo Mar 21 '10 at 21:00
  • 1
    "_It is always better to let the application crash with an unhandled exception, from which you can retrieve a stack-trace, than to silently/violently die._" How can a "crash" be better than a clean call to `terminate()`? Why don't just just call `abort()`? – curiousguy Jan 28 '17 at 17:57
41

Avoid exception specifications in C++. The reasons you give in your question are a pretty good start for why.

See Herb Sutter's "A Pragmatic Look at Exception Specifications".

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 3
    @awoodland: the use of 'dynamic-exception-specifications' (`throw(optional-type-id-list)`) is deprecated in C++11. They are still in the standard, but I guess a warning shot has been sent that their use should be considered carefully. C++11 adds the `noexcept` specification and operator. I don't know enough details about `noexcept` to comment on it. This article appears to be rather detailed: http://akrzemi1.wordpress.com/2011/06/10/using-noexcept/ And Dietmar Kühl has an article in the June 2011 Overload Journal: http://accu.org/var/uploads/journals/overload103.pdf – Michael Burr Feb 28 '12 at 22:14
  • @MichaelBurr Only `throw(something)` is considered useless and a bad idea. `throw()` is useful. – curiousguy Jan 28 '17 at 17:59
  • "_Here’s what many people think that exception specifications do: bullet Guarantee that functions will only throw listed exceptions (possibly none). bullet Enable compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown. The above expectations are, again, deceptively close to being correct_" No, the above expectations are **absolutely** correct. – curiousguy Jan 28 '17 at 18:00
14

I think the standardly except convention (for C++)
Exception specifiers were an experiment in the C++ standard that mostly failed.
The exception being that the no throw specifier is useful but you should also add the appropriate try catch block internally to make sure the code matches the specifier. Herb Sutter has a page on the subject. Gotch 82

In a addition I think it is worth describing Exception Guarantees.

These are basically documentation on how the state of an object is affected by exceptions escaping a method on that object. Unfortunately they are not enforced or otherwise mentioned by the compiler.
Boost and Exceptions

Exception Guarantees

No Guarantee:

There is no guarantee about the state of the object after an exception escapes a method
In these situations the object should no longer be used.

Basic Guarantee:

In nearly all situations this should be the minimum guarantee a method provides.
This guarantees the object's state is well defined and can still be consistently used.

Strong Guarantee: (aka Transactional Guarantee)

This guarantees that the method will complete successfully
Or an Exception will be thrown and the objects state will not change.

No Throw Guarantee:

The method guarantees that no exceptions are allowed to propagate out of the method.
All destructors should make this guarantee.
| N.B. If an exception escapes a destructor while an exception is already propagating
| the application will terminate

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • The guarantees are something every C++ programmer must know, but they don't seem to me to be related to exception specifications. – David Thornley Mar 01 '10 at 15:30
  • 1
    @David Thornley: I see the guarantees as what the exception specifications should have been (i.e. A method with the strong G can not call a method with a basic G without protection). Unfortunately I am not sure they are defined well enough to be enforced in a useful way be the compiler. – Martin York Mar 01 '10 at 16:02
  • "_Here’s what many people think that exception specifications do: - Guarantee that functions will only throw listed exceptions (possibly none). - Enable compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown. The above expectations are, again, deceptively close to being correct._" Actually, both are **exactly** right. – curiousguy Jan 25 '17 at 20:22
  • @curiousguy. That's how Java does it because it checks are enforced at compile time. C++ are runtime checks. So: `Guarantee that functions will only throw listed exceptions (possibly none)`. Not true. It only guarantees that if the function does throw these exceptions the application will terminate. `Enable compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown` Can't do that. Because I just pointed out you can't guarantee that the exception will not be thrown. – Martin York Jan 25 '17 at 20:27
  • @curiousguy: But as I said above (in 2008) exception specifications were an experiment that failed. And in C++11 exception specifications were deprecated in the language. Now we have `noexcept` to mark non-throwing methods (which act like const (you can't call a throwing method from a non-throwing method)). – Martin York Jan 25 '17 at 20:33
  • 1
    @LokiAstari "That's how Java does it because it checks are enforced at compile time_" Java exception spec is an experiment that failed. Java does not have the most useful exception spec: `throw()` (does not throw). "_It only guarantees that if the function does throw these exceptions the application will terminate_" No "not true" means true. There is no guarantee in C++ that a function does not call `terminate()`, ever. "_Because I just pointed out you can't guarantee that the exception will not be thrown_" You guarantee that the function will not throw. Which is exactly what you need. – curiousguy Jan 25 '17 at 22:27
  • from [cppreference.com: dynamic exception specification](http://en.cppreference.com/w/cpp/language/except_spec) "_Potential exceptions Each function f, pointer to function fp, and pointer to member function mfp has a set of potential exceptions, which consists of types that might be thrown. Set of all types indicates that any exception may be thrown. This set is defined as follows: 1) If the declaration of f, fp, or mfp uses throw()(deprecated) or noexcept, the set is empty._" – curiousguy Jan 25 '17 at 22:50
  • @curiousguy: N3690 is nearly two years out of date: The current version of the standard is: N4618 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4618.pdf or https://github.com/cplusplus/draft/blob/master/papers/n4618.pdf Here is a list of standard versions: http://stackoverflow.com/a/4653479/14065 – Martin York Jan 26 '17 at 22:26
  • @curiousguy: With `noexcept` the result is still a call to `terminate()`. The subtle difference from previous is that it is implementation defined if the stack is unwound. – Martin York Jan 26 '17 at 22:29
  • @curiousguy: I did no understand any of your other comments. They made no sense to me. – Martin York Jan 26 '17 at 22:32
  • I was under the impression that your sentence "_It only guarantees that if the function does throw these exceptions the application will terminate_" was only applicable to a dynamic exception spec (`throw (some,list)`) not to `nothrow`. It's the reason why I mentioned `nothrow` in my previous comment (which I have now deleted). – curiousguy Jan 27 '17 at 02:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134139/discussion-between-curiousguy-and-loki-astari). – curiousguy Jan 27 '17 at 02:23
10

gcc will emit warnings when you violate exception specifications. What I do is to use macros to use the exception specifications only in a "lint" mode compile expressly for checking to make sure the exceptions agree with my documentation.

Jeremy
  • 2,321
  • 3
  • 21
  • 24
7

The only useful exception specifier is "throw()", as in "doesn't throw".

Harold Ekstrom
  • 1,538
  • 8
  • 7
4

Exception specifications are not wonderfully useful tools in C++. However, there /is/ a good use for them, if combined with std::unexpected.

What I do in some projects is code with exception specifications, and then call set_unexpected() with a function that will throw a special exception of my own design. This exception, upon construction, gets a backtrace (in a platform-specific manner) and is derived from std::bad_exception (to allow it to be propagated if desired). If it causes a terminate() call, as it usually does, the backtrace is printed by what() (as well as the original exception that caused it; not to hard to find that) and so I get information of where my contract was violated, such as what unexpected library exception was thrown.

If I do this, I never allow propagation of library exceptions (except std ones) and derive all my exceptions from std::exception. If a library decides to throw, I will catch and convert into my own hierarchy, allowing for me to always control the code. Templated functions that call dependent functions should avoid exception specifications for obvious reasons; but it's rare to have a templated function interface with library code anyway (and few libraries really use templates in a useful manner).

coppro
  • 14,338
  • 5
  • 58
  • 73
3

A "throw()" specification allows the compiler to perform some optimisations when doing code flow analysis if it know that function will never throw an exception (or at least promises to never throw an exception). Larry Osterman talks about this briefly here:

http://blogs.msdn.com/larryosterman/archive/2006/03/22/558390.aspx

TheJuice
  • 4,434
  • 2
  • 26
  • 36
3

If you're writing code that will be used by people that would rather look at the function declaration than any comments around it, then a specification will tell them which exceptions they might want to catch.

Otherwise I don't find it particularly useful to use anything but throw() to indicate that it doesn't throw any exceptions.

Branan
  • 1,819
  • 15
  • 21
3

Yes, if you're into internal documentation. Or maybe writing a libary that others will use, so that they can tell what happens without consulting the documentation. Throwing or not throwing can be considered part of the API, almost like the return value.

I agree, they are not really useful for enforcing correctness Java style in the compiler, but it's better than nothing or haphazard comments.

user10392
  • 1,384
  • 8
  • 12
3

No. If you use them and an exception is thrown that you did not specify, either by your code or code called by your code, then the default behavior is to promptly terminate your program.

Also, I believe their use has been deprecated in current drafts of the C++0x standard.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
2

Generally I would not use exception specifiers. However, in cases where if any other exception were to come from the function in question that the program would definitively be unable to correct, then it can be useful. In all cases, make sure to document clearly what exceptions could be expected from that function.

Yes, the expected behavior of a non-specified exception being thrown from a function with exception specifiers is to call terminate().

I will also note that Scott Meyers addresses this subject in More Effective C++. His Effective C++ and More Effective C++ are highly recommended books.

Kris Kumler
  • 6,307
  • 3
  • 24
  • 27
2

They can be useful for unit testing so that when writing tests you know what to expect the function to throw when it fails, but there is no enforcement surrounding them in the compiler. I think that they are extra code that is not necessary in C++. Which ever you choose all that you should be sure of is that you follow the same coding standard across the project and the team members so that your code remains readable.

Odd
  • 4,737
  • 6
  • 30
  • 27
0

From the article:

http://www.boost.org/community/exception_safety.html

“It is well known to be impossible to write an exception-safe generic container.” This claim is often heard with reference to an article by Tom Cargill [4] in which he explores the problem of exception-safety for a generic stack template. In his article, Cargill raises many useful questions, but unfortunately fails to present a solution to his problem.1 He concludes by suggesting that a solution may not be possible. Unfortunately, his article was read by many as “proof” of that speculation. Since it was published there have been many examples of exception-safe generic components, among them the C++ standard library containers.

And indeed I can think of ways to make template classes exception safe. Unless you don't have control over all the sub-classes then you may have a problem anyway. To do this one could create typedefs in your classes that define the exceptions thrown by various template classes. This think the problem is as always tacking it on afterwards as opposed to designing it in from the start, and I think it's this overhead that's the real hurdle.

Marius
  • 3,372
  • 1
  • 30
  • 36
-2

Exception specifications = rubbish, ask any Java developer over the age of 30

Greg Dean
  • 29,221
  • 14
  • 67
  • 78
  • 9
    java programmers over the age of 30 should feel bad they couldn't cope with C, they have no excuse to be using java. – Matt Joiner Oct 26 '09 at 23:53