More Effective C++ 14: Use exception specifications judiciously
What is "exception specification?"
void newObject() throw(bad_alloc); // exception specifcation
This exception specification means "the function will only throw exceptions of type bad_alloc and nothing else". So if you try to thow an exception of another type in newObject(), the C++ compiler will complain.
Just as you might imagine, throw() means the function will never throw an exception.
However, compilers only partially check the exception specifications for you. The can only check the code in the function. If you write something like this:
void f1() throw(exception);
void f2() throw(){
....
f1();
}
the compiler won't say anything. And if f1() really throws an exception at run time, a function called unexcepted will be called, which will terminate the program.
Fortunately, C++ allows us to replace the unexcepted function.
class UnexpectedException {}; // all unexpected exception
// objects will be replaced
// by objects of this type
void convertUnexpected() // function to call if
{ // an unexpected exception
throw UnexpectedException(); // is thrown
}
set_unexpected(convertUnexpected);
With this code, you can replace the unexcepted function by convertUnexcepted().