Structured exception handling in Windows (zz)

Source: Internet
Author: User
Microsoft introduced a structured exception processing (seh) in Windows to facilitate the development of the operating system. Operating system developers use seh to make the system stronger. We can also use seh to make our programs stronger.

The following sections describe the situation:

1. End the processing program:

In Seh, try and finally are introduced to terminate the program. For the specific execution sequence, see two types of finally:

A) enter the normal control flow of the Finally block from the try block.

B) partial expansion: the force transfer from the early exit (Goto, longjump, continue, break, return) of the try block to the Finally block.

The following is an example:

Int F1 ()
{
// 1.do any processing here.
......
_ Try
{// 2.
Waitforsingleobject (g_hsem, infinite );
G_dwprotecteddata = 5;
Dwtemp = g_dwprotecteddata;
}
_ Finally
{// 3.
Releasesemaphore (g_hsem, 1, null );
}
// 4.
Return (dwtemp );
}
The above example is case A) it is executed from try to finally.

Int F2 ()
{
// 1.do any processing here.
......
_ Try
{// 2.
Waitforsingleobject (g_hsem, infinite );
G_dwprotecteddata = 5;
Dwtemp = g_dwprotecteddata;
Return (dwtemp );
}
_ Finally
{// 3.
Releasesemaphore (g_hsem, 1, null );
}
// 4.
Dwtemp = 9;
Return (dwtemp );
}

In this example, the difference between 1 and 1 is that return is added in try and dwtemp = 9 is added before return in finally, which is case B), resulting in local expansion, the execution sequence is to run the statements in finally first before the return statement is executed into try, and then the return statement in try is executed to end the process. The result returns 5 instead of 9.
2. Exception Handling Program

Besides try, finally, and try, exception.

For exceptions, the expression values can only be: prediction_continue_search, prediction_execute_handler, and prediction_execute_handler. This identifier is defined in the file excpt. h.

For exception_execute_handler, if an exception occurs, a global unwind is executed. This global expansion enables all the try and try tasks that are executed but not completed after the exception is processed, finally block recovery execution. Example:

Void F3 ()
{
// 1.do any processing here.
_ Try {// 2.
Fun ();
}
_ Handle t (/* 6. */exception_execute_handler ){
// 8.
......
}
}
Void fun (){
DWORD dwtemp = 0;
// 3 do any processing here.
_ Try {
// 4.
Waitforsingleobject (g_hsem, infinite );
// 5.
G_dwprotectdata = 5/dwtemp;
}
_ Finally {
// 7.
Releasesemaphore (g_hsem, 1, null );
}
}

For prediction_execute_handler, it means to continue executing from the command that caused the exception.

For prediction_continue_search, continue searching for exceptions in the debugging tree until it is not found as prediction_continue_search. If the filtering value of all the counter T is prediction_continue_search, It is traced back to the system's counter T and the unhandledexceptionfilter () is called (). This function displays a message box indicating that there is an unhandled exception in the thread of a process and allows the user to end or debug the process.

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.