Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
exception
bad_exception
exception
set_terminate
set_unexpected
terminate
terminate_handler
uncaught_exception
unexpected
unexpected_handler


exception

class
<exception>
class exception;

Standard exception class

Base class for standard exceptions.

All objects thrown by components of the standard library are derived from this class. Therefore, all standard exceptions can be caught by catching this type.

It is declared as:
1
2
3
4
5
6
7
8
class exception {
public:
  exception () throw();
  exception (const exception&) throw();
  exception& operator= (const exception&) throw();
  virtual ~exception() throw();
  virtual const char* what() const throw();
}


Some classes derived from exception are: bad_alloc, bad_cast, bad_exception, bad_typeid, logic_error, runtime_error, ios_base::failure...

Members

default constructor: exception () throw();
Constructs an object.
copy constructor: exception (const exception&) throw();
and assignment: exception& operator= (const exception&) throw();
Copies an exception object (the results of calling member what on the copy are implementation-specific).
destructor: virtual ~exception() throw();
Destroys the object.
const char* what() const throw();
Returns a null terminated character sequence containing a generic description of the exception.
Both the wording of such description and the character width are implementation-defined.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// exception example
#include <iostream>
#include <typeinfo>
using namespace std;
class Polymorphic {virtual void Member(){}};
int main () {
  try
  {
    Polymorphic * pb = 0;
    typeid(*pb);  // throws a bad_typeid exception
  }
  catch (exception& e)
  {
    cerr << "exception caught: " << e.what() << endl;
  }
  return 0;
}


Possible output:

exception caught: St10bad_typeid