| namespace std { //EH类型 class bad_exception; class exception; typedef void (*terminate_handler)(); typedef void (*unexpected_handler)(); // 函数 terminate_handler set_terminate(terminate_handler) throw(); unexpected_handler set_unexpected(unexpected_handler) throw(); void terminate(); void unexpected(); bool uncaught_exception(); } |
| #include <cstdio> #include <exception> using namespace std; //定义Point结构体(类) typedef struct tagPoint { int x; int y; } Point; //扔出Point异常的函数 static void f() { Point p; p.x = 0; p.y = 0; throw p; } //set_terminate将指定的函数 void terminateFunc() { printf("set_terminate指定的函数\n"); } int main() { set_terminate(terminateFunc); try { f(); //抛出Point异常 } catch (int) //捕获int异常 { printf("捕获到int异常"); } //Point将不能被捕获到,引发terminateFunc函数被执行 return 0; } |