Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::exception hierarchy

Reference for the C++ standard exception classes: logic_error, runtime_error, out_of_range, bad_alloc, bad_optional_access, and how to catch them by what().

The standard library reports errors with a family of exception classes that all derive from std::exception (header <exception>, with the concrete classes in <stdexcept> and elsewhere). The base class has one virtual member, what(), which returns a C string describing the problem.

Two big branches carry most of the meaning. std::logic_error is for mistakes a careful program could have avoided (bad arguments, out-of-range indices); std::runtime_error is for failures that only appear while running (a file that vanished, an overflow). Library features such as new, dynamic_cast, std::function and std::optional add their own classes.

C++
try { std::vector<int>{}.at(3); }
catch (const std::exception& e) { std::cout << e.what(); }

Try it

C++ Catching by type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Your own exception and nesting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

TEXT
std::exception
โ”œโ”€โ”€ std::logic_error โ”€โ”€ invalid_argument, domain_error, length_error, out_of_range, future_error
โ”œโ”€โ”€ std::runtime_error โ”€โ”€ range_error, overflow_error, underflow_error, system_error, format_error
โ”œโ”€โ”€ std::bad_alloc โ”€โ”€ bad_array_new_length
โ”œโ”€โ”€ std::bad_cast, bad_typeid, bad_function_call, bad_weak_ptr, bad_optional_access
โ”œโ”€โ”€ std::bad_variant_access, bad_expected_access<E>, bad_exception
โ””โ”€โ”€ std::ios_base::failure (derives from system_error)
C++
class exception {
public:
    virtual ~exception();
    virtual const char* what() const noexcept;
};
Class Thrown by
out_of_range vector::at, string::substr, bitset::set
invalid_argument std::stoi, std::bitset string constructor
length_error exceeding max_size()
overflow_error bitset::to_ulong and similar
bad_alloc new when memory is unavailable
bad_optional_access optional::value() on an empty object
bad_variant_access std::get with the wrong alternative
bad_function_call calling an empty std::function

Returns: what() returns text valid at least until the exception object is destroyed. The wording is not specified and differs between compilers, so never compare it to a fixed string.

Errors: what() must not throw (noexcept).

Usage notes

  • Catch by const reference, catch (const std::exception& e). Catching by value slices the object and loses the derived type and message.
  • Order catch blocks from specific to general; the first matching handler wins, so a std::exception handler placed first hides the others.
  • Derive your own types from std::runtime_error or std::logic_error, which already store the message and give a working what().
  • Constructors take std::string or const char* and copy the text; the exception can be copied without throwing.
  • std::bad_alloc is raised in low-memory conditions, so a handler should not allocate. Prefer to let it end the program or release resources.
  • Nested exceptions (throw_with_nested, rethrow_if_nested) preserve the inner cause across layers.
  • Not all errors are exceptions. std::expected, error codes (see std::error_code) and std::optional suit expected failures. Integer division by zero and null dereferences are undefined behavior, not exceptions.