From http://se.csai.cn/category.asp? Class = experteyes & page = 3
In the first episodeArticleIn the Exception Handling Model of C ++, it is described as an "object ".ProgramAnd discussed in detail the many benefits of doing so in that Article. One of them is: generally, objects are well constructed, destroyed, and replicated. This provides great support for the replication and destruction of abnormal objects in the exception handling model. Yes, but how is the so-called abnormal object copied and transmitted? Starting from this article, in the following articles, the hero, a Yu, will discuss this issue in depth and strive to find out every important detail.
Overview
Haha! Sorry, I forgot to elaborate on the definition. That is, what does "C ++ exception object is passed" mean? Everyone knows that this refers to how the exception object thrown by throw is passed to the Catch Block when an exception occurs, the exception handling module in the Catch Block then processes the exception information provided by the exception object accordingly. Programmers may think this is very simple. In fact, it is not very simple, because the transfer or replication of this object may happen between different block scopes of the same function, it may also be passed from the current function to the previous function, or it is more likely to copy the exception object to the upper (or even more layers) function.
Passing abnormal objects is similar to passing parameters in the function call process. Look! Isn't the catch keyword syntax similar to the function definition? Exception objects used as input parameters are enclosed in parentheses, except that catch can only have one parameter. Catch (...) Syntax is also a method of copying the definition of a function, indicating that any type of data objects are accepted.
In C ++ programs, function calls are implemented through "stacks". parameter transfer is also saved to the stack to achieve data sharing between two functions. What about passing abnormal objects? Of course, it is also through the stack. In fact, this is a very obvious thing, because the exception object itself must be a local variable, so it must be saved in the stack. However, after all, the transfer of abnormal objects is very different from the transfer of function parameters. The transfer of function parameters is rigorous, and the first-level object data pressure stack process and the out stack process; however, the transfer of an exception object is far more complex than this because it is in reverse order. The exception object that belongs to a local variable may need to be passed through the upper-layer (or higher-layer) function, its Process is a skip or chaotic process. How to Implement the transfer of exception objects is described in detail when analyzing the implementation of the C ++ Exception Handling Model in the secret article of love. What needs to be clarified at present is the rules or standards that need to be followed in this process.
There are three methods to pass function parameters: pointer, value, and reference. Similarly, the transfer of abnormal objects also has the difference between these three methods. At the beginning, the hero, a Yu, describes how an exception object is transmitted in each method. However, before the formal start, I would like to briefly summarize the function call process, and Stack changes in this process. This may be helpful for subsequent detailed analysis and understanding.
Function call process and "stack"
C ++ programmers must be very familiar with this process, so I will not elaborate on it here, just make a summary.
(1) The call process of the function actually uses the stack to implement the instruction (EIP) to execute the remote transfer and return process; it is supported at the CPU command level (call and RET commands );
(2) Each thread has its own stack, so the function call and execution of each thread are not affected each other;
(3) The order of function parameters in C and C ++ is generally from right to left;
(4) In C ++, function parameters are transmitted in three ways: pointer, value passing, and reference;
(5) The return values of functions in C and C ++ are generally stored in the eax register;
(6) Addressing parameters and local variables in letters in C and C ++ are generally carried out through the EBP register plus offset. For example, the parameters are generally: [EBP + XX], the local variable is generally: [EBP-XX];
(7) When the program is running, the value of EBP generally points to the current function call frame, while ESP generally points to the top of the stack.
If you have a friend who is not clear or familiar with the above discussion, you are advised to read a book dedicated to C ++ design and programming. The following shows the layout (partial fragments) of the data stored in the stack during a thread running, for example:
Summary
(1) similar to the passing of function parameters, the passing of exception objects in C ++ is also divided into three methods: pointer, value passing, and reference;
(2) What is different from the passing of function parameters is that the passing of exception objects is in the forward and backward order, and is Skip.
The next article details how to copy and pass C ++ exception objects by passing values. Do not miss it, please continue!