class
<exception>

std::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 by reference.

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();
}
1
2
3
4
5
6
7
8
class exception {
public:
  exception () noexcept;
  exception (const exception&) noexcept;
  exception& operator= (const exception&) noexcept;
  virtual ~exception();
  virtual const char* what() const noexcept;
}

Member functions


Derived types (scattered throughout different library headers)


Indirectly (through logic_error):

Indirectly (through runtime_error):

Indirectly (through bad_alloc):

Indirectly (through system_error, since C++11):

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// exception example
#include <iostream>       // std::cerr
#include <typeinfo>       // operator typeid
#include <exception>      // std::exception

class Polymorphic {virtual void member(){}};

int main () {
  try
  {
    Polymorphic * pb = 0;
    typeid(*pb);  // throws a bad_typeid exception
  }
  catch (std::exception& e)
  {
    std::cerr << "exception caught: " << e.what() << '\n';
  }
  return 0;
}

Possible output:

exception caught: St10bad_typeid