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.
try { std::vector<int>{}.at(3); } catch (const std::exception& e) { std::cout << e.what(); }
Try it
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.
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
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)
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::exceptionhandler placed first hides the others. - Derive your own types from
std::runtime_errororstd::logic_error, which already store the message and give a workingwhat(). - Constructors take
std::stringorconst char*and copy the text; the exception can be copied without throwing. std::bad_allocis 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 (seestd::error_code) andstd::optionalsuit expected failures. Integer division by zero and null dereferences are undefined behavior, not exceptions.