set_terminate function is not working for me

我是研究僧i 提交于 2021-02-16 06:25:30

问题


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?


回答1:


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++.



来源:https://stackoverflow.com/questions/4118933/set-terminate-function-is-not-working-for-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!