C++ Reference ยท Types and Language Concepts
Exception safety guarantees
Concept page on C++ exception safety: no-throw, strong, basic and no guarantee, RAII, copy-and-swap, noexcept and how standard containers behave.
Exception safety describes what state your program is in after an operation throws an exception. C++ exceptions unwind the stack, running destructors of local objects, but they do not undo changes to data that outlives the function. Code is called exception safe when it keeps its promises even if any step can throw.
The community uses four levels, introduced by David Abrahams. The no-throw guarantee says the operation never throws (it is noexcept). The strong guarantee means that if it throws, the program state is exactly as before: commit-or-rollback. The basic guarantee means that if it throws, no resources leak and every object is still valid, though its value may have changed. No guarantee means leaks or broken invariants are possible. Aim for at least the basic guarantee everywhere.
void append(std::vector<int>& v, int x) { v.push_back(x); // strong guarantee: on failure v is unchanged }
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.
Techniques
- RAII: wrap every resource (memory, files, locks, handles) in an object whose destructor releases it. Smart pointers,
std::lock_guardand containers do this, so stack unwinding cleans up for you. - Copy-and-swap: make changes on a copy, then commit with a
noexceptswap, as in the examples above. The cost is a temporary copy. - Order your operations: do the steps that can throw first, then the ones that cannot (swap, pointer assignment,
std::moveofnoexcepttypes). noexceptoperations: destructors,swap, move operations of well-behaved types,delete, and deallocation functions must not throw. Destructors are implicitlynoexcept; throwing out of one callsstd::terminate.- Move if noexcept:
std::vectorreallocation usesstd::move_if_noexcept. If your move constructor can throw and the type is copyable,vectorcopies instead, giving the strong guarantee forpush_back. Mark move operationsnoexceptwhen they truly cannot throw. - Constructors: if a constructor throws, the object never existed, so its destructor does not run, but fully constructed members and bases are destroyed. Members that own resources make this safe.
Usage notes
- Standard library: most operations give the basic guarantee at least.
std::vector::push_backandinsertof a single element at the end give the strong guarantee;std::map::insertof one element gives strong;std::swapof containers is no-throw; algorithms such asstd::sortgive only the basic guarantee (elements may be permuted). - Raw
newfollowed by something that can throw leaks. In older codef(std::unique_ptr<T>(new T), g())could leak ifgthrew between;std::make_uniqueavoids the problem, and C++17 sequencing rules mostly removed it. - Do not catch what you cannot handle. Exception safety is about being correct when exceptions propagate, not about catching all of them.
- Locks: always use
std::lock_guardorstd::scoped_lock; manualunlock()is skipped by an exception. - Testing: inject failures (throwing allocators or elements that throw on the Nth copy) and check invariants after each.
- Alternatives: where exceptions are disabled,
std::expected(C++23) carries errors in the return value; the guarantees still apply to your own state.