Windows Exception Handling _ Try _ HANDLE t

Source: Internet
Author: User
Tags try catch

 

 

Try-retry t usage

 

Try trying t is a unique exception handling model in windows. It is called Seh (structured exception handling ),

The Exception Handling Model of seh is mainly completed by the try-catch t statement, which is similar to the standard try catch statement. The catch keyword is used with the c ++ Exception Handling Model to define the Exception Handling Module, while seh is the key to adopting _ precondition T.

 

. In addition, the catch keyword is often followed by a function parameter, which can be an exception data object of various types. However, the _ struct t keyword is different, but it is followed by an expression.

 

We know that function calls are also an expression.

 

Let's take a look at the following example. This example is used to handle Stack Overflow exceptions.

Long winapi filterfunc (DWORD dwexceptioncode)

{

Return (dwexceptioncode = status_stack_overflow )? Prediction_execute_handler: prediction_continue_search;

}

 

Uint winapi threadfunc (lpvoid PARAM)

{

_ Try

{

// Guarded code

}

_ Effect (filterfunc (getexceptioncode ()))

{

// Handle stack overflow.

}

Return truet;

}

 

The following three types of parameters are supported:

Exception_continue_execution (-1) The exception is ignored. The control flow continues to run after the exception occurs.

Prediction_continue_search (0) exceptions are not identified, that is, the current _ partition t module is not the correct Exception Handling Module corresponding to this exception error. The system continues to the last try-

 

Continue to find an appropriate _ memory T module in the region T.

Exception_execute_handler (1) the exception has been identified, and the control flow enters the _ handler t module to run the exception handling code.

 

The key to try-retry T is how to obtain error information in the _ retry t module.

Windows provides two API functions to obtain exception information:

Lpexception_pointers getexceptioninformation (void); // gets exception information

DWORD getexceptioncode (void); // get the exception number

 

Getexceptioncode () returns the exception number, while getexceptioninformation () returns more information. The prediction_pointers structure is as follows,

Typedef struct _ prediction_pointers {// exp

Pexception_record exceptionrecord;

Pcontext contextrecord;

} Prediction_pointers;

 

 

Among them, the exception_record type records some exception-related information, while the Context Data Structure records the Context Environment of the thread when an exception occurs, mainly including the register value.

 

With this information, the __t t module can classify and recover abnormal errors. We usually need a filter function to help. It is usually called filterfunction. The filter function only Filters

 

Exception.

 

Int exception_access_violation_filter (lpexception_pointers p_exinfo)

{

If (p_exinfo-> predictionrecord-> predictioncode = prediction_access_violation)

{

MessageBox ("Access vialation exceptionn ");

Return exception_execute_handler; // tell the handler t to handle this exception

}

Else return exception_continue_search; // The producer t is not told to handle this exception.

}

 

Int prediction_int_divide_by_zero_filter (lpexception_pointers p_exinfo)

{

If (p_exinfo-> predictionrecord-> predictioncode = prediction_int_divide_by_zero)

{

 

Return exception_execute_handler; // tell the handler t to handle this exception

}

Else return exception_continue_search; // The producer t is not told to handle this exception.

}

 

 

Therefore, you can write the Exception Handling Code as follows:

_ Try

{

// Guarded code

}

_ Effect (exception_access_violation_filter (getexceptioninformation ()))

{

//

}

 

_ Try

{

// Guarded code

}

_ Effectexception_int_divide_by_zero_filter (getexceptioninformation ()))

{

// Exception Handling

}

 

In the seh Exception Handling model, an exception can also be thrown. The corresponding windowsapi function is raiseexception,

Void raiseexception (

DWORD dwexceptioncode, // error code

DWORD dwexceptionflags, // exception mark

DWORD nnumberofarguments, // number of parameters

Const DWORD * lparguments // The first address of the parameter Array

);

Generally, the last three parameters are not used.

Try-finally. similar to try-catch-finally in Java. however, the try of seh can only be matched with the try t and finally. Try-try t-finnaly is not allowed.

 

The C ++ exception model is defined using the try-catch syntax, while the seh exception model uses the try-catch syntax, which is similar to the C ++ exception model, the try-embedded t also supports multi-layer try-embedded t nesting.

In the try-try t model, a try block can only have one try t block, while in the C ++ exception model, a try block can have multiple catch blocks.

The C ++ exception model matches and searches based on the exception object type, while the try-try t model is different. It judges by the value of an expression.

The expression followed by the _ struct t keyword. It can be a variety of expressions. For example, it can be a function call, a conditional expression, or a comma expression, or simply an integer

 

Type constants. The most commonly used function expression is a function expression that uses the getexceptioncode () or getexceptioninformation () function to obtain the current Exception error information, so that programmers can effectively control exceptions.

 

Error classification.

In the seh Exception Handling model, an exception is thrown through the raiseexception () function. The role of the raiseexception () function is similar to the throw in the C ++ exception model.

For more details about seh Exception Handling, go to the windows via C/C ++ book. The Chinese translation is the core programming language of windows. However, we recommend that you check the original English version. The quality of the translated version is not high.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.