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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.