異常處理是一個微妙的問題,你應該盡可能優雅地處理所有異常。要達到這個目的,你需要學習terminate()函數。
terminate()函數在程序拋出一個異常并且異常沒有被捕捉的時候被調用,像下面這樣:
#include
#include
void on_terminate()
{
std::cout << "terminate() 函數被調用了!" << std::endl;
std::cin.get();
}
int main()
{
// 假如用 VC6,去掉“std::”前綴
std::set_terminate( on_terminate);
throw std::exception();
std::cout << "terminate() 函數沒有被調用!" << std::endl;
std::cin.get();
return 0;
}
避免這種情形的方案一開始看起來很簡單:
int main()
{
try
{
/* code */
}
catch( std::exception & exc)
{
// 記錄到日志,或作其他處理
}
catch(...)
{
// 記錄下“Unknown exception”
}
return 0;
}
不過,在多線程應用程序中情況變得有點復雜,因為你創建的每個線程都要有上面的(catch)處理過程。
然而terminate()函數在許多其它情況下會被調用,包括:
當你拋出一個異常,并且在它的拷貝構造函數中,另一個異常被拋出。
在堆棧展開的過程中拋出一個異常,此時析構函數拋出一個異常。
當一個靜態對象的構造函數或析構函數拋出異常時。
當一個用atexit注冊過的函數拋出一個異常時。
當你在代碼中寫下“throw;”(這意味著重新拋出當前異常),然而并沒有當前異常時。
當一個函數拋出一個它的異常說明不答應的異常時
當默認的uneXPected()處理過程被調用時
下面的代碼演示了上面各種情況下的結果:
#include
#include
void on_terminate()
{ std::cout << "terminate()函數被調用了!" << std::endl;
std::cin.get(); }
//////////////////////////////// [1]
strUCt custom_exception
{
custom_exception() {}
custom_exception( const custom_exception &)
{ throw std::exception(); }
};
void case_1()
{
try
{ throw custom_exception(); }
catch(...)
{}
}
//////////////////////////////// [2]
struct throw_in_destructor
{
~throw_in_destructor() { throw std::exception(); }
};
void case_2()
{
try
{
throw_in_destructor temp;
throw std::exception();
}
catch(...)
{}
}
//////////////////////////////// [3]
struct static_that_throws
{
static_that_throws() { throw std::exception(); }
};
void case_3()
{
// 注重:用try/catch塊包圍下面的代碼并不起作用
static static_that_throws obj;
}
//////////////////////////////// [4]
void throw_at_exit()
{ throw std::exception(); }
void case_4()
{ atexit( throw_at_exit); }
//////////////////////////////// [5]
void case_5()
{ throw; }
//////////////////////////////// [6]
class custom_6_a {};
class custom_6_b {};
void func_violating_exception_specification_6() throw(std::exception)
{ throw custom_6_a(); }
// 注重:按照我們的例子,在這個函數中我們只應該拋出
// std::exception(在函數func_violating_exception_specification
// 的定義中說明的異常);但我們沒有這樣做,
// 因此,terminate() 被調用
void on_unexpected()
{ throw custom_6_b(); }
void case_6()
{
std::set_unexpected( on_unexpected);
try
{ func_violating_exception_specification_6(); }
catch(...)
{}
}
//////////////////////////////// [7]
class custom_7 {};
void func_violating_exception_specification_7() throw(std::exception)
{ throw custom_7(); }
void case_7()
{
try
{ func_violating_exception_specification_7(); }
catch(...)
{}
}
int main()
{
std::set_terminate( on_terminate);
// 注重:確保每次僅去掉下面一個調用的注釋,
// 以便分析時將每種情況隔離開來
case_1();
// case_2();
// case_3();
// case_4();
// case_5();
// case_6();
// case_7();
return 0;
}
盡管你應該努力避免terminate()函數會被調用的情況,我們還是建議你創建自己的terminate()處理過程。你的處理過程要做的唯一合理的事是記錄一條消息到日志中。不管怎樣,確保你的日志不會拋出任何異常。
std::ostream& get_log() { /* code */ }
void on_terminate()
{
std::ostream & log = get_log();
// 確保我們不會拋出任何東西!
try
{
log.exceptions( std::ios_base::goodbit);
}
catch (...)
{}
log << "terminate() 被調用了!" << std::endl;
}
int main()
{
std::set_terminate( on_terminate) ;
// . . .
}
新聞熱點
疑難解答