set_terminate函数对我不起作用

bjs*_*123 8 c++ terminate-handler

我从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;
}
Run Code Online (Sandbox Code Playgroud)

由于代码中存在未处理的异常,因此需要调用myterminate()函数,该函数设置为终止处理程序并且应该覆盖默认的终止处理程序.

程序崩溃但没有调用myterminate().我使用的是Visual C++ 2008 Express Edition.

代码有什么问题?

Kar*_*nek 10

一种可能性 - 如果您在VC++调试器中运行程序,调试器会捕获未处理的异常,并且可能无法将控制权返回给正在运行的程序以运行myterminate.尝试在Visual C++之外运行您的程序.

  • 对.使用Ctrl + F5启动程序. (3认同)
  • @ bjskishore123:不,那是一个特色:) (2认同)