Learn about exceptions and unhandled exceptions specified in C++ programming

  • 2020-05-07 20:13:11
  • OfStack

noexcept
C++11: specifies whether the function might throw an exception.
grammar


ReturnType FunctionName(params) noexcept;
ReturnType FunctionName(params) noexcept(noexcept(expression);

parameter
expression
The result is a constant expression for True or False. The unconditional version is equivalent to noexcept(true).
note
noexcept (and its synonym noecept(true)) specifies that the function never throws an exception or allows any other function called directly or indirectly from an exception to propagate an exception. More specifically, noexcept means that the function is noexcept only if all the functions called are also noexcept or const and there is no requirement for runtime checking for potential evaluated conversions to typeid expressions or throw expressions applied to glvalue expressions of type polymorphic class types. However, the compiler will definitely check every code path that might be attributed to an exception to the noexcept function. If the exception does reach a function marked noexcept, std::terminate is immediately called, and there is no guarantee that the destructor of any scoped object will be called.
Using a function declared by the condition noexcept that evaluates to noexcept(false) specifies that it does allow exceptions to be propagated. For example, when the object to be copied is an ordinary 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 specifier throw, which is defunct in C++11 and later. We recommend that you apply noexcept to a function when you are sure that the function will never allow exceptions to propagate to the call stack. Using the functions declared by noexcept allows the compiler to generate more efficient code in many different contexts.

unhandled C++ exception
calls the predefined terminate runtime function if it cannot find a matching handler for the current exception (or the catch ellipsis handler). (you can also explicitly call terminate in any handler.) The default action for terminate is to call abort. If you want terminate to call some other function in the program before exiting the application, the set_terminate function is called with the name of the called function as a single argument to it. You can call set_terminate at any point in the program. The terminate routine always calls the last function of the parameter specified as set_terminate.
The following example throws an char * exception, but does not contain the specified handler for catching an exception of type char *. A call to set_terminate instructs terminate to call term_func.


// exceptions_Unhandled_Exceptions.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
void term_func() {
  cout << "term_func was 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 used to terminate the program or the current thread by calling exit. If it does not do this and instead returns to its caller, abort is called.


Related articles: