C++ Reference ยท Preprocessor, Tools and Concepts
Error handling strategies in C++
Compare C++ error handling strategies: exceptions, error codes, std::optional and std::expected, plus assertions and terminate, and when to use each.
C++ offers several ways to report that something went wrong, and no single one fits every situation. Exceptions separate the normal path from the failure path and cannot be ignored. Error codes and std::expected make failure part of the return type, visible in the signature and cheap on the failure path. std::optional says "a value or nothing" when the reason does not matter. Assertions and std::terminate handle bugs, which should not be "handled" at all.
A good strategy is to decide per kind of failure, not per project. Ask who can recover, how often the failure happens, and whether it is a bug or an expected condition.
std::expected<int, std::string> parse(std::string_view text); // C++23
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.
How to choose
| Situation | Good fit |
|---|---|
| Constructor cannot establish its invariant | Exception (constructors have no return value) |
| Failure is rare and the caller is several frames up | Exception |
| Failure is common and handled by the direct caller (parsing, lookup) | std::expected or std::optional |
| Value may simply be absent | std::optional |
Code must work with exceptions disabled (-fno-exceptions), embedded, or hot paths |
Error codes or std::expected |
| Violated precondition, logic bug | assert, contracts or std::terminate; do not throw |
| Unrecoverable (out of invariants, corrupted state) | std::terminate / std::abort |
Usage notes
- Exceptions need RAII. They are only safe when every resource is owned by an object whose destructor cleans up. See exception safety.
- Throw by value, catch by const reference, and derive from
std::exceptionso callers can catch generically. - Do not throw from destructors or from move operations you declare
noexcept; an escaping exception callsstd::terminate. - Exception cost model: the success path is nearly free, the throw path is slow (microseconds). Exceptions suit exceptional events, not control flow in a loop.
std::expected(C++23) carries either the value or an error of your choice.and_then,transformandor_elsechain steps. Use.value()only when you want abad_expected_accessexception.- Never ignore a status. Mark functions that return error information
[[nodiscard]]. - Pick one dominant strategy per layer. Mixing styles within one API forces callers to handle both.
- Do not catch what you cannot handle.
catch (...)that logs and continues usually hides bugs.