Windows core Programming Study Notes-Chapter 24

Source: Internet
Author: User
Tags finally block

Chapter 4 exception handling procedures and software exceptions

This chapter describes the functions of the She structure. It consists of three parts: 1. Exception classification. 2. Exception Handling program structure. 3. Differences from terminating a processing program.

I. Exception classification: hardware and software exceptions.

2. When a hardware or software exception is thrown, the OS will give our applications a chance to view the exception type and allow them to handle the exception themselves. Syntax structure of the exception handling program:

_ Try

{// Guarded body}

_ Expect T (exception filter ))

{// Exception handler (exception handler )}

A try block must be followed by only one finally t block or a Finally block. It cannot be followed by two at the same time, nor can multiple finally blocks or finally t blocks be followed. However, the try-Finally block can be nested with the try-Finally t block, which can be reversed.

When an exception is thrown, the system locates at the beginning of blocks T, and evaluates the expression of the exception filter program. The value of this expression must be one of the following three identifiers: prediction_execute_handler, prediction_execute_search, and prediction_execute_execution.

Describes how these three identifiers affect thread execution. See the following two figures.

OK. The two figures are captured from the book. The two figures are incorrect. If you are interested, try this Code:

void Func1(){__try{int j = 0;int i = 5 / j;Func2();}__except(EXCEPTION_EXECUTE_HANDLER){AfxMessageBox("Func1!\n");}}void Func2(){int dwTemp = 0;__try{int i = 5;}__finally{AfxMessageBox("Func2!\n");}AfxMessageBox("Func3!\n");}

Now I will draw the correct picture myself:

When an exception_continue_execution exception occurs, the Code continues to be executed, so it is best to avoid this type of exception.

Expand globally. Let me dictate:

Case 1: Try-try t blocks are contained outside the try-try t block. If an exception occurs in the try-try t block of the outer layer, try-try t blocks in the inner layer are not executed; if an exception occurs in the try-else t block of the internal layer, the internal layer is executed and the next line is executed.

Case 2: Try-finally blocks contain try-verify t blocks and the exception occurs in try-verify t blocks. If the try-Finally block is not executed, only blocks t are executed. In fact, this is similar to Case 1.

Case 3 (also the most complex case): the try-Finally block contains the try-Finally t block and the exception occurs in the try-Finally block. Execution sequence: Execute try-Finally block finally first, and then execute try-Finally t blocks. If the exception occurs in try-else t block, the Finally block will not be executed.

Note: Starting from Windows Vista, if an exception occurs in a try/Finally block and the outer layer has a try/try t block (at the same time, the filter returns exception_continue_handler), the process will be terminated immediately, do not expand globally or execute finally blocks.

Try the following to see if the execution order is correct? For the answer, see the code.

void Func1(){//1.do any processing here.__try{//2.call another function.Func2();}__except(/*6.Evaluate filter*/EXCEPTION_EXECUTE_HANDLE){//8.After the unwind, the exception handler executes.}//9.exception handled---continue execution.}void Func2(){DWORD dwTemp = 0;//3.do any processing here.__try{//4.request permission to access protected data.WaitForSingleObject(g_hSem, INFINITE);//5.modify the data//an exception is generated here.g_dwProtectedData = 5 / dwTemp;}__finally{//7.global unwind occurs because filter evaluated to EXCETPTION_EXECUTE_HANDLER.//allow others to use protected data.ReleaseSemaphore(g_hSem, 1, NULL);}//continue processing--never executes.}

 

Answer: The preceding table indicates the execution sequence.

Getexceptioncode is an internal function. Its return value indicates the type of the exception that has just occurred. This function can only be called in the exception filtering program (that is, in brackets after _ success t) or in the code of the exception handling program.

Comma OPERATOR: Execute comma-separated expressions from left to right. When all expressions are evaluated, return the value of the last expression.

Software exception: We can forcibly throw an exception in the program, rather than the exception thrown by the CPU to capture an event. The traditional method is: The function returns some error code to indicate that the operation fails. The caller of the function should check the error code and take corresponding measures. This causes the caller to frequently perform cleaning and return a failed code to the caller. The propagation of error codes makes it difficult to write and maintain codes.

Another way is to let the function throw an exception when it fails, rather than return the error code. In this way, the code can be easily written and maintained. The program execution efficiency is higher because many error check codes are omitted. Raiseexception can throw an exception.

Software exceptions are captured in exactly the same way as hardware exceptions. See p660.

3. Differences from terminating the handler: ① exception filter and exception handler) the operating system is mainly responsible for execution-the compiler is very limited in terms of abnormal filtering program expression calculation and exception handling program execution. ② We still remember that we recommend that you do not use return, continue, break or other statements to terminate the try block of the handler, but in the try block of the exception handler, these statements do not cause program performance loss or increase the amount of code.

 

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.