小编and*_*ipb的帖子

如果 set_terminate 中的处理程序不中止会发生什么?

如果 set_terminate 指示的处理程序本身不调用 abort(),程序的行为是什么?

如果我理解得很好,则调用 std::terminate() (在标头异常中),例如,当未捕获异常时。我读到(也在此处) std::terminate() 默认定义为对 std::abort() 的调用,但可以使用 set_terminate(handler) 进行修改。 如果新处理程序不调用 abort() 怎么办?是默认添加的吗?

我在下面说明了我不理解的行为。在一条短消息之后,terminate() 的新处理程序可以中止、调用终止或退出。如果这些选项均未设置,则程序会异常终止。但如果插入 abort() 也会发生同样的事情。如果我们使用exit(),程序会以成功结束,并在exit(..)中写入错误代码。如果我们调用terminate(),我们就会陷入无限循环(运行失败,代码127)。

这是在 Windows 8.1 计算机上使用 MinGW 6.3.0 和 NetBeans。

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 …
Run Code Online (Sandbox Code Playgroud)

c++ exception terminate

5
推荐指数
1
解决办法
793
查看次数

标签 统计

c++ ×1

exception ×1

terminate ×1