|
|
The standard C++ library defines two headers that relate to exceptions: <exception> and <stdexcept>. Exceptions are used to report error conditions. Each header is examined here.
The <exception> header defines classes, types, and functions that relate to exception handling. The classes defined by <exception> are shown here:
class exception { public: exception() throw(); exception(const bad_exception &ob) throw(); virtual ~exception() throw(); exception &operator=(const exception &ob) throw(); virtual const char *what() const throw(); }; class bad_exception: public exception { public: bad_exception() throw(); bad_exception(const bad_exception &ob) throw(); virtual ~bad_exception() throw(); bad_exception &operator=(const bad_exception &ob) throw(); virtual const char *what() const throw(); };
The exception class is a base for all exceptions defined by the C++ standard library. The bad_exception class is the type of exception thrown by the unexpected( ) function. In each, the member function what( ) returns a pointer to a null-terminated string that describes the exception.
Several important classes are derived from exception. The first is bad_alloc, thrown when the new operator fails. Next is bad_typeid. It is thrown when an illegal typeid expression is executed. Finally, bad_cast is thrown when an invalid dynamic cast is attempted. These classes contain the same members as exception.
The types defined by <exception> are as follows:
Type | Meaning |
---|---|
terminate_handler | void (*terminate_handler) ( ); |
unexpected_handler | void (*unexpected_handler) ( ); |
The functions declared in <exception> are shown here:
Function | Description |
---|---|
terminate_handler | Sets the function specified by fn as the terminate handler. A pointer to the old terminate handler is returned. |
unexpected_handler | Sets the function specified by fn as the unexpected handler. A pointer to the old unexpected handler is returned. |
void terminate( ); | Calls the terminate handler when a fatal exception is unhandled. Calls abort( ) by default. |
bool uncaught_exception( ); | Returns true if an exception is uncaught. |
void unexpected( ); | Calls the unexpected exception handler when a function throws a disallowed exception. By default, terminate( ) is called. |
The header <stdexcept> defines several standard exceptions that may be thrown by C++ library functions and/or the runtime system. There are two general types of exceptions defined by <stdexcept>: logic errors and runtime errors. Logic errors occur because of mistakes made by the programmer. Runtime errors occur because of mistakes in library functions or the runtime system and are beyond programmer control.
The standard exceptions defined by C++ caused by logic errors are derived from the base class logic_error. These exceptions are shown here:
Exception | Meaning |
---|---|
domain_error | Domain error occurred. |
invalid_argument | Invalid argument used in function call. |
length_error | An attempt was made to create an object that was too large. |
out_of_range | An argument to a function was not in the required range. |
The following runtime exceptions are derived from the base class runtime_error:
Exception | Meaning |
---|---|
overflow_error | Arithmetic overflow occurred. |
range_error | An internal range error occurred. |
underflow_error | An underflow occurred. |
|
|