C + + try-finally keyword detailed _c language

Source: Internet
Author: User
Tags exception handling finally block goto

The try-finally statement is an extension of Microsoft's C and C + + language, which enables 32-bit target programs to effectively ensure that some resources are purged when an exception occurs, and that the cleanup tasks for such resources can include, for example, release of memory, closure of files, release of file handles, and so on. Try-finally statements are especially suitable for situations such as a routine (function) where there are several places where an error needs to be detected and the function may return early when an error occurs.

#include <windows.h>
#include <stdio.h>

The syntax of the try-finally statement is similar to that of Try-except, which, slightly differently, does not have an expression after __finally because the try-finally statement is not intended for exception handling, so it does not need an expression to determine the type of current exception error. In addition, like the try-except statement, try-finally can be nested in multiple layers, and a function can have more than one try-finally statement, whether it is nested or parallel. Of course, try-finally multiple nested nesting can also be cross functional. Here is not a list of examples, you can test yourself.

In addition, is it a bit unexpected to run the results of the example program above? Because the put ("__finally block") statement in the __finally block is also executed. Yes! This is where the try-finally statement has the most magical power, that is, "in any case, the code in the Finally block area will be executed to" regardless of the circumstances in which it leaves the current scope. Oh! This is really good! To verify this rule, let's look at a more typical example with the following code:

#include <stdio.h>
void Main ()
{
puts ("Hello");
__try
{
puts ("__try block");
Notice that the following return statement directly returns the function back
;
__finally
{
puts ("__finally block");
}
Puts ("World");
}
The results of the above program run as follows:
Hello
__try block in
__finally block, press any
key to continue
void Main ()
{
Puts ("Hello");
__try
{
puts ("__try block");
}
Note that this is not a __except block, but rather a __finally replacement for
__finally
{
puts ("__finally block");
}
Puts ("World");
}

The results of the above program are as follows:

Hello
In __try block
In __finally block
World
Press any key to continue

When summarizing the flow of __finally blocks, there are three different scenarios. The first is the sequential execution of code into the __finally block area, which is simple and easy to understand; the second is that the system automatically completes the call to the __finally block code when the program flow that the GOTO statement or return statement throws away from the current __try block scope The third is that because of an exception in the __try block, the program control flow leaves the current __try block scope, in which case the system automatically completes the call to the __finally block. Whether it's a 2nd or a 3rd case, there's no doubt that they all cause a lot of overhead, and when the compiler compiles such program code, it prepares lots of extra code for both cases. In general, the 2nd case is called "Local expansion (localunwinding)", and the 3rd is called "Global Expansion (globalunwinding)". This is discussed in detail in the following elaboration of the SEH implementation.

The 3rd case, or the "global expansion" caused by an exception, may be unavoidable for programmers, because you are using the exception handling mechanism to improve the robustness of the program while inevitably causing some other overhead on performance. Oh! In fact, the world is not fair, there will be lost.

However, for the 2nd scenario, the programmer can effectively avoid it and avoid unnecessary extra overhead caused by "partial expansion." In fact, this is also consistent with the concept of structural design, that is, a program module should have only a portal and an exit, the program module to avoid the use of goto statements. However, even so, sometimes in order to improve the readability of the program, programmers may sometimes have to adopt some practices that are inconsistent with the idea of structured programming, for example, in a function where there may be multiple return statements. In this case, SEH offers a very effective compromise, which is the role of the __leave keyword, which has the same effect as the GOTO statement and return statement (due to the detection of errors in the running of a program, the need to leave the current __try block scope immediately), But it also avoids the extra overhead of "local 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");
Jump out of the current __try scope
__leave;
p = 0;
*p = +;
}
__finally
{
//will this be executed here? Of course
puts ("__finally block");
}
Puts ("World");
}
void Main ()
{
__try
{
test ();
}
__except (1)
{
puts ("__except block");
}

The results of the above program are as follows:

Hello
In __try block
In __finally block
World
Press any key to continue

The above is a small set to introduce the C + + try finally the relevant knowledge of the keyword, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.