Compartir a través de


noexcept (C++)

 

The latest version of this topic can be found at noexcept (C++).

C++11: Specifies whether a function might throw exceptions.

Syntax

ReturnType FunctionName(params) noexcept;  
ReturnType FunctionName(params) noexcept(noexcept_expression);  

Parameters

noexcept_expression
A constant expression that evaluates to true or false. The unconditional version is equivalent to noexcept(true).

Remarks

noexcept ( and its synonym noexcept(true)) specify that the function will never throw an exception or allow an exception to be propagated from any other function that it invokes either directly or indirectly. More specifically, noexcept means the function is noexcept only if all the functions that it calls are also noexcept or const, and there are no potentially evaluated dynamic casts that require a run-time check, typeid expressions applied to a glvalue expression whose type is a polymorphic class type, or throw expressions. However, the compiler does not necessarily check every code path for exceptions that might bubble up to a noexcept function. If an exception does reach a function marked noexcept, std::terminate is invoked immediately and there is no guarantee that destructors of any in-scope objects will be invoked.

A function declared with a conditional noexcept that evaluates to noexcept(false) specifies that it does permit exceptions to propagate. For example, a function that copies its argument might be declared noexcept on the condition that the object being copied is a plain old data type (POD). Such a function could be declared like this:

#include <type_traits>  
  
template <typename T>  
T copy_object(T& obj) noexcept(std::is_pod<T>)  
{  
 //. . .   
}  

Use noexcept instead of the exception specifier throw, which is deprecated in C++11 and later. We recommended you apply noexcept to a function when you are sure it will never allow an exception to propagate up the call stack. A function that is declared with noexcept enables compilers to generate more efficient code in several different contexts.

See Also

C++ Exception Handling