LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 218|回复: 0

未处理的 C++ 异常

[复制链接]
发表于 2024-2-6 22:42:36 | 显示全部楼层 |阅读模式
如果无法找到当前异常的匹配处理程序(或省略号 catch 处理程序),则调用预定义的 terminate 运行时函数。 (还可以在任何处理程序中显式调用 terminate。)terminate 的默认操作是调用 abort。 如果你希望 terminate 在退出应用程序之前调用程序中的某些其他函数,则用被调用函数的名称作为其单个自变量调用 set_terminate 函数。 您可以在程序的任何点调用 set_terminate。 terminate 例程总是调用指定为 set_terminate 的参数的最后一个函数。

以下示例引发 char * 异常,但不包含用于捕获类型 char * 的异常的指定处理程序。 对 set_terminate 的调用指示 terminate 调用 term_func。

// exceptions_Unhandled_Exceptions.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
void term_func() {
   cout << "term_func was called by terminate." << endl;
   exit( -1 );
}
int main() {
   try
   {
      set_terminate( term_func );
      throw "Out of memory!"; // No catch handler for this exception
   }
   catch( int )
   {
      cout << "Integer exception raised." << endl;
   }
   return 0;
}
输出
Output
term_func was called by terminate.
term_func 函数最好是通过调用 exit 来终止程序或当前线程。 如果它没有这样做,而是返回到其调用方,则调用 abort。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表