In some cases, the exception handling mechanism fails and a call to void terminate() is made. This terminate() call occurs in any of the following situations:
extern "C" printf(char* ...);
#include <exception>
#include <cstdlib>
using namespace std;
void f() {
printf("Function f()\n");
throw "Exception thrown from f()";
}
void g() { printf("Function g()\n"); }
void h() { printf("Function h()\n"); }
void my_terminate() {
printf("Call to my_terminate\n");
abort();
}
int main() {
set_terminate(my_terminate);
atexit(f);
atexit(g);
atexit(h);
printf("In main\n");
}
The following is the output of the above example:
In main
Function h()
Function g()
Function f()
Call to my_terminate
To register a function with atexit(),
you pass a parameter to atexit() a pointer to the function
you want to register. At normal program termination, atexit() calls
the functions you have registered with no arguments in reverse order.
The atexit() function is in the <cstdlib> library.The terminate() function calls the function pointed to by terminate_handler. By default, terminate_handler points to the function abort(), which exits from the program. You can replace the default value of terminate_handler with the function set_terminate().
A terminate function cannot return to its caller, either by using return or by throwing an exception.
Related information