C ++ keywords (try-finally)

Source: Internet
Author: User
Tags finally block

The try-finally statement is Microsoft's extension to the C and C ++ languages. It enables 32-bit target programs to effectively clear some resources in time when exceptions occur, the cleanup tasks of these resources can include the release of memory, the closure of files, and the release of file handles. The try-finally statement is especially suitable for this scenario. For example, in a routine (function), there are several places where an error needs to be detected. When an error occurs, the function may return it in advance.

# Include <windows. h>
# Include <stdio. h>

The syntax of a try-finally statement is similar to that of a try-finally statement. The difference is that __finally does not have an expression after it, because the try-finally statement is not used for exception processing, therefore, it does not need an expression to determine the type of the current Exception error. In addition, like the try-Finally t statement, try-finally can also be nested in multiple layers, and a function can have multiple try-finally statements, whether nested, or parallel. Of course, try-finally multi-layer nesting can also be cross-function. Examples are not listed here. You can test them yourself.
In addition, do you feel a little unexpected about the running results of the above sample program? Because the put ("_ Finally block") Statement in the _ Finally block is also executed. Yes, that's right! This is where the try-finally statement has the most magical power, that is, "No matter under what circumstances, when you leave the current scope, code in the Finally block area will be executed ". Haha! This is really amazing! To verify this rule, the following is a more typical example. The Code is as follows:

# Include <stdio. h>

Void main ()
{
Puts ("Hello ");
_ Try
{
Puts ("_ Try block ");

// Note that the following return statement directly causes the function to return
Return;
}
_ Finally
{
Puts ("_ Finally block ");
}

Puts ("World ");
}

The program running result is as follows:
Hello
_ Try Block
_ Finally block
Press any key to continue

 

Void main ()
{
Puts ("Hello ");
_ Try
{
Puts ("_ Try block ");
}
// Note: Here it is not _ blocks t, but _ finally replaced
_ Finally
{
Puts ("_ Finally block ");
}

Puts ("World ");
}

The program running result is as follows:
Hello
_ Try Block
_ Finally block
World
Press any key to continue

There are three situations when the _ Finally block is executed. The first type is to sequentially execute the code in the _ Finally block area. This situation is simple and easy to understand; the second is that when the program control flow caused by the GOTO statement or return statement leaves the current _ Try block scope, the system automatically calls the _ Finally block code; the third is that when an exception occurs in the _ Try block, the control flow of the program leaves the current _ Try block scope, in this case, the system automatically calls the _ Finally block. Whether it is 2nd or 3rd cases, there is no doubt that they will cause a lot of system overhead. When the compiler compiles such program code, it will prepare a lot of additional code for both cases. Generally, it is referred to as "localunwinding" and "globalunwinding" in 2nd cases )". This will be analyzed in detail when we discuss seh implementation later.
In 3rd cases, that is, global expansion caused by exceptions, this may be unavoidable for programmers, this is because when you use the exception handling mechanism to improve program reliability and robustness, it will inevitably lead to other performance overhead. Haha! In fact, the world is fair, and there are gains and losses.

However, in 2nd cases, programmers can effectively avoid it and avoid unnecessary overhead caused by "Local expansion. This is also consistent with the structured program design philosophy, that is, a program module should have only one entry and one exit, and the GOTO statement should be avoided in the program module as much as possible. However, even so, sometimes to improve the readability of the program, the programmer may have to adopt some practices that are contrary to the structured program design idea when writing code. For example, in a function, there may be multiple return statements. In this case, seh provides a very effective compromise, that is, the role of the _ leave keyword, it has the same effect as a GOTO statement and a return statement (because an error in a program running is detected, You need to immediately leave the current _ Try block scope ), however, this avoids the additional overhead of "partial expansion. Let's look at an example! The Code is as follows:

# Include <stdio. h>

Void test ()
{
Puts ("Hello ");
_ Try
{
Int * P;
Puts ("_ Try block ");

// Directly jump out of the current _ Try Scope
_ Leave;
P = 0;
* P = 25;
}
_ Finally
{
// Will it be executed here? Of course
Puts ("_ Finally block ");
}

Puts ("World ");
}

Void main ()
{
_ Try
{
Test ();
}
_ Partition T (1)
{
Puts ("_ blocks t ");
}
}

The program running result is as follows:
Hello
_ Try Block
_ Finally block
World
Press any key to continue

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.