自定義 C++ 中未捕獲的異常終止行為
在本教程中,我們將討論一個程式,用於自定義 C++ 中未捕獲異常的行為。
通常,try-catch 塊處理異常,但在有些情況下卻沒有匹配的 catch 塊,並且程式只是中止。這個 terminate() 函式可以根據使用者要求進行修改。
示例
#include <exception> #include <iostream> using namespace std; //defining custom terminator void myhandler(){ cout << "Inside new terminate handler\n"; abort(); } int main(){ set_terminate(myhandler); try { cout << "Inside try block\n"; throw 100; } catch (char a){ cout << "Inside catch block\n"; } return 0; }
輸出
Inside try block Inside new terminate handler
廣告