2012-04-07 50 views
2
#include "iostream" 
#include "conio.h" 
#include "exception" 
#include "cstdlib" 
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"; } 
    getch(); 
    return 0; 
} 

輸出(當在Visual Studio 2008中執行): 捕獲其他異常異常處理 - set_unexpected()不能調用

但是,我期待(不符合標準的編譯器?)輸出是:

意外稱爲

抓INT

注:我在Visual Studio中執行該計劃2008年

回答

2

是,按照標準輸出應該是[#1]

意外稱爲
抓INT

gcc給出accurate result

請注意,MSVC是臭名昭着的w.r.t處理異常規範。異常規範被認爲是failed experiment
AFAIK,MSVC不實現異常規範,除了空的(throw()/nothrow

C++ 03標準:

[#1] 15.5.2的unexpected()函數[except.unexpected ]

unexpected()函數不應該返回,但它可以拋出(或重新拋出)異常。如果它拋出異常規範允許的以前違反的新異常,那麼搜索另一個處理程序將繼續在異常規範被違反的函數的調用處...

+1

Thanks Man: - ):D ;-) – Jatin 2012-04-07 18:12:45