Analysis of exception handling in Visual C ++ (5)

Source: Internet
Author: User
4. structured exception handling

Structured exception handling (seh) is an extension of Microsoft's Windows program exception handling. in Visual C ++, it supports both C and C ++ languages. Seh should not be mixed with standard C ++ Exception Handling and MFC Exception Handling. For C ++ programs, Microsoft recommends using standard C ++ exception handling.

To support Seh, four keywords are defined in Visual C ++, which are not necessarily supported by other compilers. These keywords are used to extend the C and C ++ languages:

(1) _ limit t

(2) _ finally

(3) _ leave

(4) _ Try

The basic syntax is:

_ Try
{
... // The monitored code block that may cause exceptions
}
_ Partition T (filter-expression)
{
... // Exception Handling Function
}

Or:

_ Try
{
...
}
_ Finally
{
... // Terminate
}

The procedure is as follows:

(1) _ Try block executed;

(2) If the _ Try block does not have an exception, it will be executed after _ blocks t; otherwise, it will be executed to _ blocks t, the exception handling method is determined based on the value of filter-expression:

A. The value of filter-expression is prediction_continue_execution (-1)

Recover an exception. Execute the exception from the exception. The exception handling function is not executed;

B. The value of filter-expression is prediction_continue_search (0)

The exception is not identified, and an exception is not caught. Search for the next exception processing function;

C. The value of filter-expression is prediction_execute_handler (1)

The exception is identified and aborted. the stack is rolled back from where the exception occurred, and all the termination functions encountered along the way are executed.

Let's take a look at this example:

// Example 4-1
# Include "stdio. H"

Void main ()
{
Int * P = NULL; // defines a null pointer.
Puts ("seh begin ");
_ Try
{
Puts ("in try ");
_ Try
{
Puts ("in try ");
* P = 0; // a memory access exception is thrown.
}
_ Finally
{
Puts ("in finally ");
}
}
_ Partition T (puts ("in filter"), 1)
{
Puts ("in bytes t ");
}
Puts ("seh end ");
}

Program output:

Seh begin
In try // execute _ Try Block
In try // execute the embedded _ Try Block
In filter // execute filter-expression and return prediction_execute_handler
In finally // expand the embedded _ finally
In bytes t // execute the corresponding _ blocks t
Seh end // process completed

If we change _ partition T (puts ("in filter"), 1) to _ partition T (puts ("in filter"), 0), the output of the program will be changed:

Seh begin
In try // execute _ Try Block
In try // execute the embedded _ Try Block
In filter // execute filter-expression and return prediction_continue_search
In finally // expand the embedded _ finally

The execution of the program also crashes. the dialog box shown in 3 is displayed.


Figure 3 seh cannot be correctly executed

To run this program correctly, we can set another _ Try block and a _ Try block that receives the filter-expression and returns the value of prediction_execute_handler, the program is changed:

// Example 4-2
# Include "stdio. H"

Void main ()
{
Int * P = NULL; // defines a null pointer.
Puts ("seh begin ");
_ Try
{
_ Try
{
Puts ("in try ");
_ Try
{
Puts ("in try ");
* P = 0; // a memory access exception is thrown.
}
_ Finally
{
Puts ("in finally ");
}
}
_ Partition T (puts ("in filter"), 0)
{
Puts ("in bytes t ");
}
}
_ Partition T (puts ("in filter"), 1)
{
Puts ("in bytes t ");
}
Puts ("seh end ");
}

Program output:

Seh begin
In try // execute _ Try Block
In try // execute the embedded _ Try Block
In filter1 // execute filter-expression and return prediction_continue_search
In filter2 // execute filter-expression and return prediction_execute_handler
In finally // expand the embedded _ finally
In bytes T2 // execute corresponding _ blocks t
Seh end // process completed

It can be seen that, because the filter-expression of the first _ partition t returns prediction_continue_search, "in sequence T1" is not output. The program did not crash because it finally encountered the 2nd _ handler t that received prediction_execute_handler.

The complexity of seh is that it is difficult to control the flow direction of exception handling. If it is difficult to solve the problem, the program will "fail. If we change _ partition T (puts ("in filter") in Example 4-1 to _ partition T (puts ("in filter"),-1 ), the program will enter an endless loop and output:

Seh begin
In try // execute _ Try Block
In try // execute the embedded _ Try Block
In filter // execute filter-expression and return prediction_continue_execution
In Filter
In Filter
In Filter
In Filter
... // Output "in filter"

Finally, the "in filter" is output frantically. We set the breakpoint before the _ partition T (puts ("in filter"),-1) Statement, and press f5.

5. Comparison of various Exception Handling Methods

The following table compares the four types of exception handling supported by Visual C ++ in this article:

Exception Handling Supported languages Standard or not Complexity Recommended
C Exception Handling C Language Standard C Simple Recommendation
C ++ Exception Handling C ++ Language Standard C ++ Relatively simple Recommendation
MFC Exception Handling C ++ Language Only for MFC programs Relatively simple Not recommended
Seh Exception Handling C and C ++ languages Only for Microsoft compiling Environments Complicated Not recommended

This article only describes the preliminary knowledge of Visual C ++ Exception Handling. For more in-depth content, we also need to understand and learn in the continuous programming process.

During the program design process, we cannot handle exceptions as "troublesome" and ignore or ignore possible errors. This avoids the "trouble" of exception handling and will cause greater "trouble" to our programs ". The program contains necessary exception handling, which is also a basic requirement for a good programmer.

 

Previous Page [1] [2] [3] [4] [5]

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.