Skip to content
Tutorials Center
My learning

Topics

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.

C++
void append(std::vector<int>& v, int x) {
    v.push_back(x);       // strong guarantee: on failure v is unchanged
}

Try it

C++ Basic versus strong guarantee

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++ RAII prevents leaks, copy-and-swap gives the strong guarantee

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_guard and containers do this, so stack unwinding cleans up for you.
  • Copy-and-swap: make changes on a copy, then commit with a noexcept swap, 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::move of noexcept types).
  • noexcept operations: destructors, swap, move operations of well-behaved types, delete, and deallocation functions must not throw. Destructors are implicitly noexcept; throwing out of one calls std::terminate.
  • Move if noexcept: std::vector reallocation uses std::move_if_noexcept. If your move constructor can throw and the type is copyable, vector copies instead, giving the strong guarantee for push_back. Mark move operations noexcept when 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_back and insert of a single element at the end give the strong guarantee; std::map::insert of one element gives strong; std::swap of containers is no-throw; algorithms such as std::sort give only the basic guarantee (elements may be permuted).
  • Raw new followed by something that can throw leaks. In older code f(std::unique_ptr<T>(new T), g()) could leak if g threw between; std::make_unique avoids 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_guard or std::scoped_lock; manual unlock() 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.