According to the C++ Standard, [except.spec], paragraph 8 [ISO/IEC 14882-2014]:
A function is said to allow an exception of type
E
if the constant-expression in its noexcept-specification evaluates tofalse
or its dynamic-exception-specification contains a typeT
for which a handler of typeT
would be a match (15.3) for an exception of typeE
.
If a function throws an exception other than one allowed by its exception-specification, it can lead to an implementation-defined termination of the program ([except.spec], paragraph 9).
If a function declared with a dynamic-exception-specification throws an exception of a type that would not match the exception-specification, the function std::unexpected()
is called. The behavior of this function can be overridden but, by default, causes an exception of std::bad_exception
to be thrown. Unless std::bad_exception
is listed in the exception-specification, the function std::terminate()
will be called.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <vector> void f(std::vector<int> &v, size_t s) { v.resize(s); // May throw, but that is okay } |
Implementation Details
Some vendors provide language extensions for specifying whether or not a function throws. For instance, Microsoft Visual Studio provides __declspec(nothrow))
, and Clang supports __attribute__((nothrow))
. Currently, the vendors do not document the behavior of specifying a nonthrowing function using these extensions. Throwing from a function declared with one of these language extensions is presumed to be undefined behavior.
...
Bibliography
[GNU] | Declaring Attributes of Functions | ||
[ISO/IEC 14882-2014] | 15.4, "Exception Specifications" | ||
[MSDN] | nothrow (C++) | [GNU] | Declaring Attributes of Functions |
...