Noexcept
c++11: Specifies whether the function may throw an exception.
Grammar
ReturnType functionname (params) noexcept;
ReturnType functionname (params) noexcept (noexcept (expression);
Parameters
An expression
A constant expression that evaluates to True or False. An unconditional version is equivalent to Noexcept (true).
Note
Noexcept (and its synonym noecept (true)) specifies that a function will never throw an exception, or allow any other function that is called directly or indirectly from an exception to propagate the exception. More specifically, noexcept means that when all functions invoked are also noexcept or const and are not required to run-time checks, typeid expressions that apply to Glvalue expressions of type polymorphic class types, or potential evaluated conversions of throw expressions, the function It's noexcept. However, the compiler does not necessarily check each code path that may be attributed to the exception of the Noexcept function. If the exception does arrive at a function marked as noexcept, then the std::terminate is called immediately, and no guarantee will be invoked to invoke the destructor of any scoped object.
A function declared with the condition noexcept and evaluates to Noexcept (false) specifies that it does allow propagating exceptions. For example, when the object being copied is a normal old data type (POD), the function that copies its arguments can be declared as noexcept. This type of function can be declared as follows:
#include <type_traits>
template <typename t>
T copy_object (t& obj) noexcept (std::is_pod<t >)
{
//...
}
Use noexcept instead of the exception descriptor throw, which has been deprecated in c++11 and later versions. When you are sure that the function will never allow an exception to propagate to the call stack, we recommend that you apply the noexcept to the function. Functions declared using noexcept enable the compiler to generate more efficient code in many different contexts.
Unhandled C + + exceptions
If a matching handler (or an ellipsis catch handler) for the current exception cannot be found, the predefined terminate Run-time function is invoked. (You can also explicitly call terminate in any handler.) The default action for terminate is to call abort. If you want terminate to invoke some other functions in your program before exiting the application, call the Set_terminate function with the name of the called function as a single argument. You can call set_terminate at any point in the program. The terminate routine always invokes the last function of the parameter specified as set_terminate.
The following example throws a char * exception, but does not contain the specified handler for the exception that captures the type char *. A call to set_terminate instructs terminate to invoke Term_func.
Exceptions_unhandled_exceptions.cpp
//compile with:/EHsc
#include <iostream>
using namespace STD;
void Term_func () {
cout << "Term_func is called by terminate." << Endl;
Exit (-1);
}
int main () {
try
{
set_terminate (term_func);
Throw "Out of memory!"; No catch handler for this exception
}
catch (int)
{
cout << "Integer exception raised." < < Endl;
}
return 0;
}
Output:
Term_func was called by terminate.
The Term_func function is best to terminate the program or the current thread by calling exit. If it does not, but returns to its caller, call abort.