extern "C" printf(char* ...);
#include <exception>
#include <cstdlib>
using namespace std;
extern "C" void f() {
printf("Function f()\n");
throw "Exception thrown from f()";
}
extern "C" void g() { printf("Function g()\n"); }
extern "C" 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");
}
See 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.