Structed Exception Handler 學習總結(二)

1. try-except語句的執行流程

try-except語句的一般形式:

__try

{

    //guarded section

}

__except(filter expression)

{

    //exception handler

}

首先,明確幾個術語,__try子句後面的複合語句被稱作guarded section,__except子句括號中的表達式稱爲filter expression,__except子句後面的複合語句稱爲exception handler

執行流程如下:

(1)guarded section被執行

(2)如果執行(1)時未發生異常,則直接執行exception handler複合語句後面的語句

(3)如果執行(1)時發生了異常(注意:發生的這個異常可以是guarded section中的代碼引發的,也可以是guarded section中調用其他函數,從而由該函數引發的),此時filter expression就會被計算(evaluated),filter expression的計算結果有三個值,這三個值決定了該異常應該被如何處理:

a. EXCEPTION_CONTINUE_EXECUTION (1) 這個值表示該異常被忽略,不調用exception handler,同時將程序的執行控制流程轉到異常發生的指令處繼續執行,實際上這時死循環就出現了。執行控制流程轉到異常發生的指令處繼續執行,又引發異常,filter expression被求值,得到EXCEPTION_CONTINUE_EXECUTION,程序又被轉到異常發生的指令處繼續執行,如此循環往復,從而出現了死循環。所以這個值在使用的時候要特別的小心。

b. EXCEPTION_CONTINUE_SEARCH (0) 這個值表示在調用堆棧中繼續搜索合適的exception handler,當前的exception handler不會被調用。如果,filter expression的計算結果是這個值,實際上表明這個exception並不是當前exception handler所要處理的exception,所以,當前的exception handler 不能也不應該被調用。

c. EXCEPTION_EXECUTE_HANDLER (1) 這個值表示此時應該調用當前的exception handler來處理guarded section中發生的exception,並且當調用完exception handler之後,程序如果沒有被返回(return),則程序繼續執行exception handler複合語句後面的語句。這個值說明當前的exception handler就是爲處理guarded section中發生的exception所準備的,自然exception handler就被調用了。

所以,綜上所述只有當filter expression被計算爲EXCEPTION_EXECUTE_HANDLER時,當前的exception handler纔會被調用,否則exception handler是不會被調用的。

注意:a.  Because the __ except expression is evaluated as a C expression, it is limited to a single value, the conditional-expression operator, or the comma operator. If more extensive processing is required, the expression can call a routine that returns one of the three values listed above.

b.  The filter expression of a frame-based exception handler is an expression that is evaluated by the system when an exception occurs within the guarded body. This evaluation results in one of the following actions by the system.

(1) The system stops its search for an exception handler, restores the machine state, and continues thread execution at the point at which the exception occurred.

(2) The system continues its search for an exception handler.

(3) The system transfers control to the exception handler, and thread execution continues sequentially in the stack frame in which the exception handler is found.If the handler is not in the stack frame in which the exception occurred, the system unwinds the stack, leaving the current stack frame and any other stack frames until it is back to the exception handler's stack frame. Before an exception handler is executed, termination handlers are executed for any guarded bodies of code that terminated as a result of the transfer of control to the exception handler.

 

2. 當一個異常被try-excpt語句捕獲並處理了之後,這個try-ecept語句塊中就沒那種被捕獲的異常了。也就是說如下代碼中最外層的__excep塊永遠不會被執行

     __try

     {

         __try

         {

              Div(1, 0, &r);//拋出一個除0異常

         }

         __except(EXCEPTION_EXECUTE_HANDLER)

         {

              _tprintf_s(TEXT("main: __except.\n"));

              return -1;

         }

     }

     __except(EXCEPTION_EXECUTE_HANDLER)

     {

         _tprintf_s(TEXT("maimn out: __except.\n"));

         return FALSE;

     }

 

3. 編寫exception handler代碼時的一些限制:

The principal limitation to using exception handlers in code is that you cannot use a goto statement to jump into a __try statement block. Instead, you must enter the statement block through normal flow of control. You can jump out of a __try statement block and nest exception handlers as you choose.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章