C++ throw()引发的core

22 篇文章 0 订阅

总结最近遇到了一个问题,简单说就是一个禁止抛出异常的函数内抛出了异常所致(无论函数内部直接抛出异常还是间接调用函数抛出异常)

官方文档说明如下:

 If this throw specifier is left empty with no type, this means that std::unexpected is called for any exception. Functions with no throw specifier (regular functions) never call std::unexpected, but follow the normal path of looking for their exception handler.

1
2
int myfunction (int param) throw(); // all exceptions call unexpected
int myfunction (int param);         // normal exception handling 

即一个throw()的函数意味着对任何异常调用std::unexpected

std::unexpected函数的官方文档说明如下:

By default, the unexpected handler calls terminate. But this behavior can be redefined by calling set_unexpected.

This function is automatically called when a function throws an exception that is not listed in its dynamic-exception-specifier (i.e., in its throw specifier).

This function is provided so that the unexpected handler can be explicitly called by a program, and works even ifset_unexpected has not been used to set a custom unexpected handler (calling terminate in this case).

即该函数默认为调用terminate函数

 

而terminate函数的说明如下:

By default, the terminate handler calls abort. But this behavior can be redefined by calling set_terminate.

This function is automatically called when no catch handler can be found for a thrown exception, or for some other exceptional circumstance that makes impossible to continue the exception handling process.

This function is provided so that the terminate handler can be explicitly called by a program that needs to abnormally terminate, and works even if set_terminate has not been used to set a custom terminate handler (callingabort in this case).

即该函数默认调用abort函数

 

故一个throw为空的函数内部有任何异常抛出的效果是导致程序调用abort

 

简单的代码示例如下:
 

voidfunc2() { //一个抛出异常的函数
    throw 2;
}
void func1() throw() {  //一个禁止抛出异常的函数
    func2();
}
int main(int argc, char ** argv) {
    func1();
    return 0;
}

 

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值