How to handle C ++ exceptions?

Source: Internet
Author: User

When the C ++ exception handler finds the catch Block in the function, it first needs to determine whether the exception is located in the try block of the function where the current function has an exception. If yes, find the catch Block table related to the try block. Otherwise, return directly.

First, let's see how to find the try block. During compilation, the compiler assigns the start id and end id to each try block. Through the funcinfo structure, the exception handler can access these two IDs, as shown in figure 4. The compiler generates a data structure for each try block in the function.

In the previous section, I mentioned that C ++ Exception Handling adds an id field to the EXCEPTION_REGISTRATION structure. Recall 3: This structure is located on the function stack. When an exception occurs, the handler reads this value to see if it is in the range [start id, end id] determined by the two IDs of the try block. If yes, the exception occurs in the try block; otherwise, you can continue to view the next try block in the try block table.

Who is responsible for updating the value of the id and what should it be? Originally, the compiler inserts code in multiple locations of the function to update the id value to reflect the real-time running status of the program. For example, the compiler adds a statement into the try block and writes the start id of the try block to the stack shard.

After the try block is found, the handler traverses the catch Block table associated with it to check whether there are catch blocks that are interested in the current exception. When a try block is nested, the exception is caused by both the try block in the inner layer and the try block in the outer layer. In this case, the handler should search for catch blocks in the first internal and external order. But it does not need to care about this, because in the try block table, the C ++ Exception Handling always puts the inner try block before the outer try block.

Another difficulty for exception handling programs is "how to determine whether the catch Block is willing to handle the current exception based on the data structure of the catch Block ". This is done by comparing the exception type and the type of the catch Block parameter. For example, the following program:

 
 
  1. void foo()   
  2. {   
  3.     try   
  4.     {   
  5.         throw E();   
  6.     }   
  7.     catch(H)   
  8.     {   
  9.         //.   
  10.     }   
  11. }  

If H and E are of the same type, the catch block must catch this exception. This means that the handler must perform type comparison at runtime. For languages such as C, this is impossible because they cannot get the object type at runtime.

C ++ exception handling is different. It has runtime type identification (RTTI) and provides a standard method for comparing runtime types. C ++ Exception Handling defines the type_info class in the standard header file, which can be a table type in the runtime.

The second field ptype_info of the catch block data structure, as shown in figure 4) is a pointer to the type_info structure, which represents the parameter type of the catch block at runtime. The = operator is also overloaded with type_info to indicate whether the two types are identical. In this way, the exception handler only needs to compare and call the = Operator) The type_info Of The catch Block parameter can be accessed through the data structure of the catch Block) and whether the type_info of the exception is the same.

You can see if the catch Block is willing to catch the current exception. The parameter type of the catch block can be obtained through the funcinfo structure, but where does the exception type_info come from? When the compiler encounters.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.