gpt4 book ai didi

c++ - set_terminate 函数对我不起作用

转载 作者:可可西里 更新时间:2023-11-01 17:39:46 29 4
gpt4 key购买 nike

我从 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;
}

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

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

代码有什么问题?

最佳答案

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

关于c++ - set_terminate 函数对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4118933/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com