Namespaces
Variants
Views
Actions

noexcept specifier (since C++11)

From cppreference.com
< cpp‎ | language
Revision as of 14:33, 20 August 2013 by Arbalest (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

Specifies whether a function will throw exceptions or not.

Contents

Syntax

noexcept (1)
noexcept(expression) (2)

Explanation

If the value of the constant expression is true, the function is declared to not throw any exceptions. noexcept without a constant expression is equivalent to noexcept(true).

One of the uses of the constant expression is (along with the noexcept operator) to define templated functions that declare noexcept for some types but not others.

Note that a noexcept specification on a function is not a compile-time check; it is merely a method for a programmer to inform the compiler whether or not a function should throw exceptions. The compiler can use this information to enable certain optimizations on non-throwing functions as well as enable the noexcept operator, which can check at compile time if a particular expression is declared to throw any exceptions. For example, containers such as std::vector will move their elements if the elements' move constructor is noexcept, and copy otherwise (unless the copy constructor is not accessible, but a potentially throwing move constructor is, in which case the strong exception guarantee is waived).

If a function marked noexcept allows an uncaught exception to escape at runtime, std::terminate is called immediately.

Deprecates

noexcept is an improved version of throw(), which is deprecated in C++11. Unlike throw(), noexcept will not call std::unexpected and may or may not unwind the stack, which potentially allows the compiler to implement noexcept without the runtime overhead of throw().

Keywords

noexcept

Example

// whether foo is declared noexcept depends on if the expression
// T() will throw any exceptions
template <class T>
  void foo() noexcept(noexcept(T())) {}
void bar() noexcept(true) {}
void baz() noexcept { throw 42; }  // noexcept is the same as noexcept(true)
 
int main() 
{
    foo<int>();  // noexcept(noexcept(int())) => noexcept(true), so this is fine
 
    bar();  // fine
    baz();  // compiles, but at runtime this calls std::terminate
}

See also

noexcept operator(C++11) determines if an expression throws any exceptions[edit]
Dynamic exception specification(until C++17) specifies what exceptions are thrown by a function (deprecated in C++11) [edit]
throw expression signals an error and transfers control to error handler[edit]
converts the argument to an xvalue if the move constructor does not throw
(function template) [edit]