2012-04-07 13 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; 
} 

出力を呼び出すことができません: は他の例外をキャッチ例外処理 - set_unexpected()(のVisual Studio 2008上で実行される)

しかし、私は期待していた(非準拠のコンパイラ?)出力は次のようにします

予想外呼ば

はint型

をキャッチ

注:私は、Visual Studio上で、このプログラムを実行し、2008年

答えて

2

はい、標準ごとに出力があるべき[#1]

予想外呼ば
キャッチint型

gccは、accurate resultとなる。

MSVCは、例外仕様を処理することはよく知られています。例外仕様は、failed experimentとみなされます。
私の知る限り、MSVCが空のもの(throw()/nothrow

C++ 03標準を除いて、例外の仕様を実装していない:

[#1] 15.5.2 unexpected()関数[except.unexpected ]

unexpected()関数が返してはならないが、それは例外をスロー(または再スロー)することができます。以前に違反していた例外仕様で許可されている新しい例外がスローされた場合、例外仕様が破られた関数の呼び出しで別のハンドラの検索が続行されます....

+1

ありがとう - ):D ;-) – Jatin

関連する問題