1
香草薩特在他的文章http://www.gotw.ca/gotw/047.htm 指出,我們不能在desturctor功能使用uncaught_exception,爲什麼不能在dtor中使用uncaught_exception?
// Why the wrong solution is wrong
//
U::~U() {
try {
T t;
// do work
} catch(...) {
// clean up
}
}
如果U形物體被摧毀,由於期間的異常傳播堆棧展開,T ::〜T將失敗儘管它可以安全地使用「可能拋出的代碼」路徑。
,但我寫了一個測試程序,以及T ::〜T IN其實並沒有使用「代碼會拋出異常」
#include <exception>
#include <iostream>
using namespace std;
class T {
public:
~T() {
if(!std::uncaught_exception())
{
cout<<"can throw"<<endl;
throw 1;
} else
{
cout<<"cannot throw"<<endl;
}
}
};
struct U
{
~U() {
try
{
T t;
}
catch(...)
{
}
}
};
void f()
{
U u;
throw 2;
}
int main()
{
try
{
f();
}
catch(...)
{}
}
輸出是: 不能扔
我錯過什麼?
感謝
感謝您的詳細解釋conio – camino 2011-01-07 10:04:20