Standard exceptions in C++

All the standard exceptions in C++ are derived from the exception class that is defined in the exception header file. The standard exceptions are defined in the stdexcept header file.

The hierarchy of standard exceptions is shown below using an ASCII tree:

├── bad_alloc
│   └── bad_array_new_length
├── bad_cast
├── bad_exception
├── bad_typeid
├── logic_error
│   ├── domain_error
│   ├── future_error
│   ├── invalid_argument
│   ├── length_error
│   └── out_of_range
└── runtime_error
    ├── overflow_error
    ├── range_error
    ├── system_error
    │   └── ios_base::failure
    └── underflow_error
  • Typically, logic_error, runtime_error and their children are thrown in user code. These exceptions take a std::string or string literal as input.

  • You cannot throw exception or any of the language exceptions. You can however derive from the exception class to create your own exception.

Reference: § in The C++ Programming Language (4 Ed) by Stroustrup

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s