set_unexpected使用

VS2005中的set_unexpected与标准C++不同,在exception头文件中set_unexpected函数定义为空,MSDN对set_unexpected的解释如下

The C++ Standard requires that unexpected is called when a function throws an exception that is not on its throw list.The current implementation does not support this. The following example calls unexpected directly, which then calls the unexpected_handler.

inline unexpected_handler __CRTDECL set_unexpected(unexpected_handler)
	_THROW0()
	{	// register an unexpected handler
	return 0;
	}

// exception_set_unexpected.cpp
// compile with: /c /EHsc
#include<exception>
#include<iostream>

using namespace std;

void unfunction( ) 
{
    cout << "I'll be back." << endl;
    terminate( );
}

int main( ) 
{
    unexpected_handler oldHand = set_unexpected( unfunction );
    unexpected( );
}

在LINUX下运行情况如下

// set_unexpected example
#include <iostream>
#include <exception>
using namespace std;

void myunexpected () {
    cerr << "unexpected called\n";
    throw 0;     // throws int (in exception-specification)
}

void myfunction () throw (int) {
    throw 'x';   // throws char (not in exception-specification)
}

int main (void) {
    set_unexpected (myunexpected);
    try {
        myfunction();
    }
    catch (int) { cerr << "caught int\n"; }
    catch (...) { cerr << "caught other exception (non-compliant compiler?)\n"; }
    return 0;
}

//result:
//unexpected called
//caught int



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值