Apache C++ Standard Library Reference Guide

## exception

Library: Language support

    Does not inherit

- Local Index
- Summary
- Synopsis
- Interface
- Example
- Constructors
- Destructor
- Public Member Operator
- Public Member Function
- See Also
- Standards Conformance

### Local Index

#### Members

exception() | operator=() | what() | ~exception()

### Summary

Class exception is the base class that supports logic and runtime errors.

### Synopsis

    #include <exception>

    namespace std {
      class exception;
    }

### Interface

    namespace std {
          class exception {
          public:
            exception() throw();
            exception(const exception&) throw();
            exception& operator=(const exception&) throw();
            virtual ~exception() throw();
            virtual const char* what() const throw();
          };
    }

### Example

    //
    // except.cpp
    //

    #include <iostream>
    #include <stdexcept>

    #ifndef _RWSTD_NO_EXCEPTIONS

    int main ()
    {
       try {
           // Enable exceptions in cin.
           std::cin.exceptions(std::ios::eofbit);

           // Clear all bits and set eofbit.
           std::cin.clear(std::ios::eofbit);
       }
       catch (const std::ios::failure &e) {
           std::cout << "Caught an exception: "
                     << e.what () << std::endl;
       }
       catch (const std::exception &e) {
           std::cout << "Caught an exception: "
                     << e.what () << std::endl;

           return 1;   // Indicate failure.
       }
       catch (...) {
           std::cout << "Caught an unknown exception"
                     << std::endl;

           return 1;   // Indicate failure.
       }

       return 0;
    }

    #else

    int main ()
    {
        std::cout << "Exceptions not supported." << std::endl;

        return 0;
    }

    #endif   // _RWSTD_NO_EXCEPTIONS

    Program Output:

    Caught an exception: std::cin: stream object has set ios::eofbit

### Constructors

    exception() throw();

Default constructor. Constructs an object of class exception.

    exception(const exception&) throw();

Copy constructor. Copies an exception object.

### Destructor

    virtual ~exception() throw();

Destructor. Destroys an object of class exception.

### Public Member Operator

    exception&
    operator=(const exception&) throw();

Assignment operator. Copies an exception object.

### Public Member Function

    virtual const char*
    what() const throw();

Returns an implementation-defined, null-terminated byte string representing a human-readable message describing the exception. The message may be a null-terminated multibyte string, suitable for conversion and display as a wstring.

### See Also

Exceptions, bad_alloca, bad_cast, bad_exception, bad_typeid, domain_error, invalid_argument, ios_base::failure, length_error, logic_error, out_of_range, overflow_error, range_error, runtime_error, underflow_error

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 19.1