我想玩處理意外的異常,但不能使它工作。這是自例如:C++ Reference意外的事情發生了
// 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;
}
他們說,輸出應該是: 輸出:
意外稱爲
抓INT
,當我嘗試這不會發生。我的輸出是:
捕獲其他異常
我使用VS2010 SP1
「發生了意想不到的事情。」是的,例外情況就是這樣。 – 2011-02-10 09:21:47
在`g ++`中,這種行爲是正確的;也許這是VS2010中的一個怪癖? – templatetypedef 2011-02-10 09:32:15