如果使用set_terminate指示的处理程序本身不调用abort(),程序的行为是什么?
如果我理解的话,会调用std :: terminate()(在标头异常中),例如,当没有捕获到异常时。我read(也here)std :: terminate()默认定义为对std :: abort()的调用,但可以使用set_terminate(handler)进行修改。 如果新处理程序没有调用abort()怎么办?它是默认添加的吗?
我在下面说明了我不理解的行为。在短消息之后,terminate()的新处理程序可以中止,或者调用terminate,或退出。如果未设置这些选项,程序将以异常终止结束。但是如果插入了abort(),也会发生同样的事情。如果我们使用exit(),程序将以成功结束,并在exit(..)中写入错误代码。如果我们调用terminate(),我们会得到一个无限循环(运行失败,代码127)。
这是使用NetBeans的Windows 8.1计算机上的MinGW 6.3.0。
void myOwnOnExit() {
cerr << "called myOwnOnExit\n";
}
void myOwnTerminate() {
cerr << "called myOwnTerminate\n";
// Uncomment one of the following:
// // if none is uncommented, abnormal termination, error 3
// abort(); // with or without it, abnormal termination, error 3
// terminate(); // get an infinite loop, error code 127 in 3 seconds
// exit(EXIT_SUCCESS); // displays "called myOwnOnExit", success
}
int main() {
atexit(myOwnOnExit);
set_terminate(myOwnTerminate);
throw 1;
cerr << "we should not see this"; // and we don't
}
非常感谢您的任何暗示或暗示。
答案 0 :(得分:2)
您应该在terminate_handler
中终止该程序,这是标准所要求的:
必需的行为:terminate_handler将终止程序的执行而不返回调用者。
因此,如果您的处理程序无法满足要求,则它是未定义的行为。