Exceptions. McGraw-Hill The Complete Reference Fourth Edition
Reklam
Reklam
926
C + + : T h e C o m p l e t e R e f e r e n c e
T his chapter describes the exception handling classes. It also describes the auto_ptr and pair classes, and gives a brief introduction to the localization library.
Exceptions
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.
<exception>
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
C h a p t e r 3 8 : E x c e p t i o n H a n d l i n g a n d M i s c e l l a n e o u s C l a s s e s
927 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:
Type terminate_handler unexpected_handler
Meaning void (*terminate_handler) ( ); void (*unexpected_handler) ( );
The functions declared in <exception> are shown in Table 38-1.
<stdexcept>
The header <stdexcept> defines several standard exceptions that may be thrown by C++ library functions and/or its run-time system. There are two general types of exceptions defined by <stdexcept>: logic errors and run-time errors. Logic errors occur because of mistakes made by the programmer. Run-time errors occur because of mistakes in library functions or the run-time system, and are beyond programmer control.
Function terminate_handler set_terminate(terminate_handler fn) throw( ); unexpected_handler set_unexpected(unexpected_handler fn) throw( ); void terminate( ); bool uncaught_exception( ); void unexpected( );
Description
Sets the function specified by fn as the terminate handler. A pointer to the old terminate handler is returned.
Sets the function specified by fn as the unexpected handler. A pointer to the old unexpected handler is returned.
Calls the terminate handler when a fatal exception is unhandled. Calls
abort( )
by default.
Returns true if an exception is uncaught.
Calls the unexpected exception handler when a function throws a disallowed exception. By default, terminate( ) is called.
Table 38-1.
The Functions Defined Within <exception>
İndir
Reklam