Exception Handling in C++

This page might seem like it duplicates some of what we have just seen, but it is valuable because it gives a different perspective on the topic. Read chapter 1 on pages 15-60.

Exception specifications

Better exception specifications?

You may feel that the existing exception specification rules aren't very safe, and that

void f();
should mean that no exceptions are thrown from this function. If the programmer wants to throw any type of exception, you might think he or she should have to say
void f() throw(...); // Not in C++
This would surely be an improvement because function declarations would be more explicit. Unfortunately, you can't always know by looking at the code in a function whether an exception will be thrown ­– it could happen because of a memory allocation, for example. Worse, existing functions written before exception handling was introduced into the language may find themselves inadvertently throwing exceptions because of the functions they call (which might be linked into new, exception-throwing versions). Hence, the uninformative situation whereby
void f();
means, "Maybe I'll throw an exception, maybe I won't". This ambiguity is necessary to avoid hindering code evolution. If you want to specify that f throws no exceptions, use the empty list, as in:
void f() throw();