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