Exception Specifications
An optional exception specification (also called a throw list) enumerates a list of exceptions that a function can throw. For example, consider the function declaration
int someFunction( double value ) throw ( ExceptionA, ExceptionB, ExceptionC ) { // function body }
In this definition, the exception specification, which begins with keyword throw immediately following the closing parenthesis of the function's parameter list, indicates that function someFunction can throw exceptions of types ExceptionA, ExceptionB and ExceptionC. A function can throw only exceptions of the types indicated by the specification or exceptions of any type derived from these types. If the function tHRows an exception that does not belong to a specified type, function unexpected is called, which normally terminates the program.
A function that does not provide an exception specification can tHRow any exception. Placing throw()an empty exception specificationafter a function's parameter list states that the function does not tHRow exceptions. If the function attempts to throw an exception, function unexpected is invoked. Section 16.7 shows how function unexpected can be customized by calling function set_unexpected.
Common Programming Error 16.7
Throwing an exception that has not been declared in a function's exception specification causes a call to function unexpected. |
Error-Prevention Tip 16.3
The compiler will not generate a compilation error if a function contains a throw expression for an exception not listed in the function's exception specification. An error occurs only when that function attempts to throw that exception at execution time. To avoid surprises at execution time, carefully check your code to ensure that functions do not throw exceptions not listed in their exception specifications. |