自定义 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
广告
Data Structure
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP