天天看點

vc中異常捕捉的最後一道屏障-SetUnhandledExceptionFilter

在c++中定義了很多異常捕捉機制,但是在vc中在win平台上有一個更高層的異常處理機制,函數setunhandledexceptionfilter,這個函數很有用,它是異常捕捉的最後一道屏障。它有這樣的規則,對于未捕捉的異常并且未在調試狀态下(通常就是隻在release中),将自動調用這裡面的異常處理函數。

預設的異常處理函數就是彈出一個對話框,告訴你程式異常了,而在釋出軟體時,你可以用其他函數替代,優雅的結束程式。這個處理函數的定義是這樣的:

long winapi myunhandledexceptionfilter(_exception_pointers* pexceptioninfo)

{

 ...

return

}

這個函數有三種傳回值,來表示不同的程式行為:

exception_execute_handler

0x1

return from unhandledexceptionfilter and execute the associated exception handler. this usually results in process termination.

exception_continue_execution

0xffffffff

return from unhandledexceptionfilter and continue execution from the point of the exception. note that the filter function is free to modify the continuation state by modifying the exception information supplied through itslpexception_pointers

parameter.

exception_continue_search

0x0

proceed with normal execution of unhandledexceptionfilter. that means obeying theseterrormode flags, or invoking the application error pop-up message box.

第一種就是悄悄的終止程式了

第二種就是從這個異常處繼續執行

第三種就是調用預設的方法,可能就是彈出一個框,當然這個預設的方法通常是遵循seterrormode 的模式。

其實标準c++中也有一個類似的函數set_unexpected,差別是,這個函數的執行情況比較有限制,隻是當c++ 函數抛出一個不在它自己參數清單中的異常時才會觸發,局限性比較大。

繼續閱讀