Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set_terminate function is not working for me

I have the following code taken from cplusplus.com:

// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;

void myterminate () {
  cout << "terminate handler called\n";
  abort();  // forces abnormal termination
}

int main (void) {
  set_terminate (myterminate);
  throw 0;  // unhandled exception: calls terminate handler
  return 0;
}

As there is unhandled exception in the code, it needs to call myterminate() function which is set as terminate handler and supposed to override the default terminate handler.

The program is crashing but not calling myterminate(). I am using Visual C++ 2008 Express Edition.

What's the issue with the code?

like image 605
bjskishore123 Avatar asked Nov 07 '10 17:11

bjskishore123


1 Answers

One possibility - if you are running the program inside VC++ debugger, the debugger catches unhandled exceptions and it might not return control back to the running program to run myterminate. Try to run your program outside Visual C++.

like image 173
Karel Petranek Avatar answered Sep 30 '22 13:09

Karel Petranek