Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The noexcept specifier and operator

Reference for C++ noexcept: the specifier, the conditional form, the noexcept operator, why move operations need it, and what happens on a throw.

noexcept has two jobs. As a specifier on a function, it promises that the function will not let an exception escape. If one does, std::terminate is called instead of unwinding. As an operator, noexcept(expr) asks the compiler at compile time whether an expression is declared non-throwing.

The promise lets the compiler skip unwinding bookkeeping and lets library code choose faster strategies. The most important example: std::vector moves its elements when it grows only if the element's move constructor is noexcept; otherwise it copies them, to keep its strong exception guarantee.

C++
void swap(Widget& a, Widget& b) noexcept;
Widget(Widget&&) noexcept;

Try it

C++ The specifier and the operator

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++ Why move constructors should be noexcept

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

C++
void f() noexcept;                    // same as noexcept(true)
void g() noexcept(constant-expr);    // conditional
bool b = noexcept(expression);        // operator, unevaluated
Form Description
noexcept / noexcept(true) The function does not exit via an exception. If one tries, std::terminate runs; stack unwinding may or may not happen first.
noexcept(false) The function may throw. This is the default for ordinary functions.
noexcept(expr) operator Yields true if expr contains no potentially-throwing call, no throw, no dynamic_cast on references that could fail, and so on. The operand is not evaluated.

The specifier is part of the function type since C++17, so a pointer to a noexcept function converts to a plain function pointer but not the other way round.

Usage notes

  • Destructors and deallocation functions are noexcept by default; so are implicitly declared special members when all their parts are.
  • Mark move constructors, move assignment operators and swap as noexcept whenever they truly cannot throw. Containers and std::move_if_noexcept rely on it.
  • Do not add noexcept just because exceptions "should not happen": a throw inside terminates the program, which may be exactly what you want only for unrecoverable errors.
  • Functions that allocate (new, std::string, push_back) can throw std::bad_alloc; marking them noexcept means running out of memory ends the program.
  • Narrow-contract functions (those with preconditions) are often left without noexcept so that tests can throw on misuse.
  • Dynamic exception specifications throw(int) were removed in C++17; throw() was removed in C++20. Use noexcept.
  • noexcept does not change overload resolution, but virtual overriders may not be less strict than the base function: a base noexcept virtual cannot be overridden by a throwing one.
  • Conditional noexcept(noexcept(expr)) is the standard idiom for generic wrappers.