VC ++ exception handling of usage __try __except

Reprinted: https://blog.csdn.net/jiaxiaokai/article/details/50983867

__try __except usage:

__try __except windows system is a unique exception handling model, called SEH (structured exception handling), SEH exception handling model is mainly done by __try __except statement, similar to the standard try catch.

And exception handling model using C ++ catch keyword to define an exception handling module, and is employed __except keyword SEH defined above; and catch behind often takes a function key parameter may be various types of abnormal data objects;

The __except keyword is different, with behind it is an expression.

example:

Copy the code
long WINAPI FilterFunc(DWORD dwExceptionCode)
{
   return (dwExceptionCode == 
   STATUS_STACK_OVERFLOW) ? 
   EXCEPTION_EXECUTE_HANDLER : 
  EXCEPTION_CONTINUE_SEARCH;
}

UINT WINAPI ThreadFunc(LPVOID param)
{
  __try
 {
       // guarded code 
 }
 __except (FilterFunc(GetExceptionCode()))
 {
   // 如果是栈溢出,进行处理。
  }
return 0;
}
Copy the code

 

Value except the following three parameters:

       EXCEPTION_CONTINUE_EXECUTION (-1):           the exception is ignored, the control flow will occur after the abnormal point, continue the recovery operation.

  EXCEPTION_CONTINUE_SEARCH (0):                  no abnormality is recognized, i.e., the current block is not the exception error __except corresponding correct exception handling module.

                                                                                            The system will continue to __try __except domain continue to find an appropriate __except module.

  EXCEPTION_EXECUTE_HANDLER (1):                 abnormality has been identified, the control flow proceeds to the operation module __except exception handling code

 

The key is how to get __try __except exception error relevant information __except module?

Windows provides two API functions to get the exception information:

Copy the code
LPEXCEPTION_POINTERS GetExceptionInformation (VOID); // abnormality information obtaining 

DWORD GetExceptionCode (VOID); // number acquired abnormality 

EXCEPTION_POINTERS structure is as follows,
typedef struct _EXCEPTION_POINTERS {// exp PEXCEPTION_RECORD ExceptionRecord; pContext ContextRecord; } EXCEPTION_POINTERS;
Copy the code

EXCEPTION_RECORD type, which records some information related to the anomaly;

CONTEXT data structure recorded in an exception occurs, then the thread contexts, including the value of the register.

With this information, __ except module can be good for exception error classification and recovery process, we usually need a filter function to assist. Is generally referred filterfunction. Require filtration at the filtering function only

Abnormal reasonable.

Copy the code
exception_access_violation_filter int (LPEXCEPTION_POINTERS p_exinfo) 
{ 
    IF (p_exinfo-> ExceptionRecord-> == EXCEPTION_ACCESS_VIOLATION the ExceptionCode) 
    { 
        MessageBox ( "Access vialation exceptionn"); 

       return EXCEPTION_EXECUTE_HANDLER is; // tell the exception processing except 
    } 
    the else 
    { 
     return EXCEPTION_CONTINUE_SEARCH is; // not tell except to handle the exception 
    } 
} 

int exception_int_divide_by_zero_filter (LPEXCEPTION_POINTERS p_exinfo) 
{ 
    IF (p_exinfo-> ExceptionRecord-> == EXCEPTION_INT_DIVIDE_BY_ZERO the ExceptionCode) 
    { 
        return EXCEPTION_EXECUTE_HANDLER is; // tell the exception processing except 
    } 
   the else 
   {
    return EXCEPTION_CONTINUE_SEARCH; // do not tell except handle the exception 
   } 
}
Copy the code

So, you can write this exception handling code:

Copy the code
the __try 
{ 

  // the guarded code 

} 

the __except (exception_access_violation_filter (the GetExceptionInformation ())) 
{ 

 // this program can be called BugReport 

}
Copy the code

 

 SEH exception handling model, you can also throw an exception. WindowsAPI corresponding function is RaiseException,

Copy the code
The RaiseException VOID ( 

DWORD dwExceptionCode, abnormal numbers // 

DWORD dwExceptionFlags, // abnormality flag 

DWORD nNumberOfArguments, // number parameter 

CONST DWORD * lpArguments // first address parameter array 

);
Copy the code

 

Typically, after three basic parameters without SEH exception handling as well as __try __finally. Similar to java in the try-catch-finally. But the only try of SEH and a match between the two except and finally, can not have try- except-finnaly.

C ++ exception model by try-except try-catch syntax definition, but with the exception model SEH try-except syntax similar to C ++ exception model, try-except also supports multiple nesting.

try-except model, there is only one a try block except block; the C ++ exception model, a try block can have multiple catch blocks.

C ++ exception model according to the type of the exception object is to find a match; try-except the model is different, it is determined to be a value with an expression __except back keyword expression, which may be of various types. expression,

For example, it can be a function call or a conditional expression, expression or a comma, or simply is an integer constant, and so on.

The most commonly used is a function expression, and by using GetExceptionCode () or the GetExceptionInformation () function to obtain the current exception error information to facilitate effective control of the programmer exception error classification process.

SEH exception handling model, the abnormality by the RaiseException () function throws. The RaiseException () function returns similar to C ++ exception model throw.

Guess you like

Origin www.cnblogs.com/ethan0929/p/10983646.html