C++ throw()引发的core

来源:互联网 发布:淘宝集运香港仓电话 编辑:程序博客网 时间:2024/05/29 05:56

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

官方文档说明如下:

 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.

12
int myfunction (int param) throw(); // all exceptions call unexpectedint 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;
}

 

运行结果:

 

0 0
原创粉丝点击