總結最近遇到了一個問題,簡單說就是一個禁止拋出異常的函數內拋出了異常所致(無論函數內部直接拋出異常還是間接調用函數拋出異常)
官方文檔說明如下:
If this throw
specifier is left empty with no type, this means
that std::unexpected is called for any exception. Functions
with no throw
specifier (regular functions) never call std::unexpected,
but follow the normal path of looking for their exception handler.
|
|
即一個throw()的函數意味着對任何異常調用std::unexpected
而std::unexpected函數的官方文檔說明如下:
By default, the unexpected handler calls terminate.
But this behavior can be redefined by calling set_unexpected.
This function is automatically called when a function throws an exception that is not listed in its dynamic-exception-specifier (i.e.,
in its throw
specifier).
This function is provided so that the unexpected handler can
be explicitly called by a program, and works even ifset_unexpected has
not been used to set a custom unexpected handler (calling terminate in
this case).
即該函數默認爲調用terminate函數
而terminate函數的說明如下:
By default, the terminate handler calls abort.
But this behavior can be redefined by calling set_terminate.
This function is automatically called when no catch
handler can be found for a thrown exception, or for some other
exceptional circumstance that makes impossible to continue the exception handling process.
This function is provided so that the terminate handler can
be explicitly called by a program that needs to abnormally terminate, and works even if set_terminate has
not been used to set a custom terminate handler (callingabort in
this case).
即該函數默認調用abort函數
故一個throw爲空的函數內部有任何異常拋出的效果是導致程序調用abort。
簡單的代碼示例如下:
voidfunc2() { //一個拋出異常的函數
throw 2;
}
void func1() throw() { //一個禁止拋出異常的函數
func2();
}
int main(int argc, char ** argv) {
func1();
return 0;
}
運行結果: