The pair Class

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 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
   set_terminate(terminate_handler fn)
       throw( );

Sets the function specified by fn as the terminate handler. A pointer to the old terminate handler is returned.

unexpected_handler
   set_unexpected(unexpected_handler fn)
       throw( );

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.

<stdexcept>

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.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net