A function declared noexcept promises that no exception will escape it. In C++, this non-throwing declaration gives callers and library code information they can use when choosing how to handle an operation.
That promise has teeth: if an exception tries to leave a non-throwing function, the program calls std::terminate. Understanding how C++ uses noexcept helps you decide where that consequence is justified and where an exception should remain free to reach a caller.
noexcept C++ declarations: making the promise
Put the specifier after the function’s parameter list. Both declarations below express the same non-throwing promise:
void reset() noexcept;
void clear() noexcept(true);
A declaration with noexcept(false) is potentially throwing, much like an ordinary function declaration without an explicit specifier. It does not mean that the function must throw. It means that an exception is allowed to propagate to a caller.
The C++ noexcept specifier describes an interface, so matching declarations and definitions must agree on the exception specification. Since C++17, the specification also forms part of a function type. A pointer to a non-throwing function can be converted to a pointer to a potentially throwing function, but not the other way around: a caller relying on the stronger promise needs that promise to hold.
A non-throwing function may call a potentially throwing one. It can even catch an exception from that call and continue or return a fallback. What matters is whether an exception escapes the outer boundary. Think of the function as a room where exceptions may enter, provided none leave by the door.
What happens when the promise breaks?
Consider a function that calls an operation which might fail:
void risky_operation();
void perform() noexcept {
risky_operation();
}
If risky_operation() throws and perform() does not handle the exception, the search for a handler reaches the boundary of perform(). The C++ noexcept rule calls std::terminate; a handler around the call to perform() cannot recover from that escaping exception. The default terminate handler calls std::abort, ending the program.
Do not rely on local objects being destroyed during this particular failure: whether the stack is unwound before termination is implementation-defined. If cleanup is essential, arrange for the operation to handle failures before they reach the boundary. A catch block can log an error and return a status only if those actions themselves respect the function’s non-throwing promise.
This distinction matters for an API. A function that reports failure by throwing should normally remain potentially throwing. Marking it non-throwing does not turn the exception into an error code; it changes the consequence of an exception escaping.
Conditional promises and the operator
The expression in a noexcept specification must be a constant expression convertible to bool. This lets a template make its promise depend on the type it receives. C++ also has a noexcept operator that checks, at compile time, whether an expression is potentially throwing:
#include <type_traits>
#include <utility>
template<class T>
void swap_values(T& a, T& b)
noexcept(std::is_nothrow_swappable_v<T>) {
using std::swap;
swap(a, b);
}
static_assert(noexcept(swap_values(
std::declval<int&>(), std::declval<int&>())));
This C++ noexcept example uses C++17’s is_nothrow_swappable_v. The specification follows the trait for T; the operator in static_assert asks about a particular call. The checked expression is not executed. Its result comes from the language’s rules and declared exception specifications, not a test run through every possible input.
You may also see a specification written as noexcept(noexcept(expression)). The outer form declares the promise; the inner operator asks whether the expression is potentially throwing. The repeated word looks like a typing accident, but each occurrence has a different job. Make sure the expression being checked corresponds to the actual operation in the body, including overload selection.
Why moves and containers care
Moving an object can transfer its resources more cheaply than copying them. During a std::vector reallocation, however, a throwing move could leave some original elements already moved from if a later move fails. To preserve the strong exception guarantee where possible, the vector can copy elements when moving may throw and copying is available.
The noexcept C++ mechanism gives the library the information needed to choose. A non-throwing move constructor enables the vector to move elements while maintaining that guarantee. If a type cannot be copied and its move may throw, the vector may have to use the move anyway; if that move throws, the strong guarantee can be waived.
struct Buffer {
Buffer(Buffer&& other) noexcept;
Buffer& operator=(Buffer&& other) noexcept;
// Other members and resource management omitted.
};
The declarations alone do not make these operations safe. Their implementations must really avoid letting exceptions escape. For a class composed of standard RAII members, consider defaulting the move operations and letting their exception specifications follow the members. Check the resulting traits when generic code depends on them.
The noexcept C++ library utility std::move_if_noexcept illustrates the same decision: it yields an rvalue reference when moving is non-throwing or copying is unavailable, and otherwise yields a const lvalue reference so a copy can be selected. It does not itself move an object; the operation that consumes its result makes that choice.
A promise is not a proof of correctness
A non-throwing function can return a wrong answer, violate a precondition, leak a resource, or encounter undefined behavior. In C++, noexcept says only that an exception will not propagate past the function boundary. The compiler can still accept a call to a potentially throwing operation inside it, so inspect every path that might allow an exception out.
A function can also handle an exception internally and report failure through a return value. Document that result separately: callers need to know what the value means and what state remains after failure. Tests for normal behavior and failure paths remain valuable, even when a declaration supplies a clear exception guarantee.
Should you add it everywhere?
No. Use the noexcept C++ specifier when you can uphold the promise across the function’s implementation and expect callers or generic code to benefit. Move operations, swaps, and cleanup operations are common candidates. Leave an operation potentially throwing when callers should be able to catch its failures, and revisit the declaration whenever its implementation gains a new failure path.
