C++未经处理的异常

如果匹配的处理程序 (或省略号 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 调用。

请参见

参考

C++异常处理