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.
void swap(Widget& a, Widget& b) noexcept; Widget(Widget&&) noexcept;
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
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
noexceptby default; so are implicitly declared special members when all their parts are. - Mark move constructors, move assignment operators and
swapasnoexceptwhenever they truly cannot throw. Containers andstd::move_if_noexceptrely on it. - Do not add
noexceptjust 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 throwstd::bad_alloc; marking themnoexceptmeans running out of memory ends the program. - Narrow-contract functions (those with preconditions) are often left without
noexceptso that tests can throw on misuse. - Dynamic exception specifications
throw(int)were removed in C++17;throw()was removed in C++20. Usenoexcept. noexceptdoes not change overload resolution, but virtual overriders may not be less strict than the base function: a basenoexceptvirtual cannot be overridden by a throwing one.- Conditional
noexcept(noexcept(expr))is the standard idiom for generic wrappers.