Exception Handling in C ++ and windows (try catch; _ Try _ try t; _ Try _ finally)

Source: Internet
Author: User
Tags finally block
  • C ++ Exception Handling: Try, catch
try
{
     // statements that may be wrong, if there are errors, throw ...
     // initialize an exception object
}
catch (type name [formal parameter name]) // exception specifier
{
     // do exception handling
}
catch (type name [formal parameter name])
{
     // do exception handling
} 


The exception handling in C ++ is very simple, that is, the above three keywords. Note that throw in C ++ does not have finally in Java or other languages after catch.

Q: Why does C ++ not provide a "finally" structure?
A: Because c ++ provides another mechanism that can completely replace finally, and this mechanism almost always works better than finally: "allocating resources is initialization ".
(See section 14.4 of the C ++ programming language) The basic idea is to use a local object to encapsulate a resource, in this way, the destructor of a local object can automatically release resources. In this way, programmers will not "forget to release resources.


  • Windows seh Exception Handling: __try, _ Wait t

These are seh models provided on Windows operating systems. That is to say, Windows APIs are actually called when called in C ++.
Seh, also known as structured exception handling, is a way to handle exceptions when designing a Windows operating system. This set of exception handling mechanisms is very similar to C ++, except that the keyword is catch rather than catch.

Catch and catch t are different:
The catch keyword is often followed by a function parameter. It can be an exception data object of various types, but the _ struct t keyword is different, it is followed by an expression (it can be an expression of various types ).


Summary:

(1) The C ++ exception model is defined using the try catch syntax, while the seh exception model uses the _ Try _ try t syntax;
(2) similar to the C ++ exception model, __try _ partition t also supports multi-layer _ Try _ partition t nesting.
(3) Unlike the C ++ exception model, in the __try _ try t model, A _ Try block can only have one _ try t block; in the C ++ exception model, a try block can have multiple catch blocks.
(4) similar to the C ++ exception model, in the __try _ abnormal T model, the rules for searching the exception module are also carried out step by step. However, the difference is that the C ++ exception model performs a matching search based on the exception object type, while the _ Try _ try t model is different, it is determined by the value of an expression. If the expression value is 1 (prediction_execute_handler), the exception handling module is found. If the value is 0 (prediction_continue_search ), it indicates to continue searching for other exception handling modules that may match in the _ Try _ expect t domain of the previous layer. If the value is-1 (prediction_continue_execution), it indicates to ignore this exception, note that this value is rarely used because it can easily lead to unpredictable results, such as endless loops and even program crashes.
(5) The expression following the _ comment t keyword. It can be a variety of expressions. For example, it can be a function call, a conditional expression, or a comma expression, or an integer constant. The most commonly used function expression is a function expression that uses the getexceptioncode () or getexceptioninformation () function to obtain the current Exception error information, so that programmers can effectively control the classification of exception errors.
(6) In the seh Exception Handling Model, exceptions are classified into two categories: system exceptions and software exceptions. The software exception is thrown through the raiseexception () function. The role of the raiseexception () function is similar to the throw statement in the C ++ exception model.

See: http://www.cnblogs.com/wenziqi/archive/2010/08/26/1809074.html for details



  • Windows seh Exception Handling: __try, _ finally

The syntax of the _ Try _ finally statement is similar to that of the _ Try _ Finally t statement. The difference is that there is no expression after __finally, this is because the _ Try _ finally statement is not used for exception handling, so it does not need an expression to determine the type of the current Exception error. In addition, similar to the _ Try _ partition t statement, __try _ finally can also be nested in multiple layers, and a function can have multiple _ Try _ finally statements, whether it is nested or parallel. Of course, __try _ finally multi-layer nesting can also be cross-function.

The most critical point is: "under whatever circumstances, code in the __finally block area will be executed when it leaves the current scope ".


Summary:

_ When a Finally block is executed, there are three situations:

(1) execute the code in the _ Finally block area sequentially. This situation is simple and easy to understand;
(2) When the program control flow caused by a GOTO statement or return statement leaves the current _ Try block scope, the system automatically calls the _ Finally block code;
(3) 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 )".
In 3rd cases, that is, global expansion caused by exceptions, this may be unavoidable for programmers, because the exception handling mechanism is used to improve program reliability and robustness, it will inevitably lead to other performance overhead. There must be loss.



Remarks: This article Reprinted from: http://www.cppblog.com/yehao/articles/165099.html


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.