1. Introduction
In the "exception handling in C + +" article (see Computer World Network December 20, 2001), we discussed exception (or exception) processing in C + +. This article will explore further the structural exception handling in Visual C + +.
Imagine how enjoyable it would be if you didn't have to think about any errors in the programming process, your program would never go wrong, there was enough memory, and the files you needed were always there. At this point your program does not need too many if statements to go around, very easy to write, easy to read, and easy to understand. If you think such a programming environment is a dream, then you will like structural exception handling (STRUCTU Reed exception handling).
The essence of structural exception handling is to keep you focused on how to accomplish your task. If any errors occur during the running of the program, the system receives (catch) and notifies (notify) you. Although you cannot completely ignore the possibility of error in your program by using structural exception handling, structured exception handling does allow you to separate your main tasks from error handling. This separation allows you to focus on your work and consider possible mistakes in the future.
The main work of structural exception handling is done by the compiler, not by the operating system. The compiler needs to generate extra special code to support structural exception handling when it encounters an exception segment. Therefore, each compiler product vendor may use its own syntax and regulations. Here we use Microsoft's Visual C + + compiler for discussion.
Be careful not to confuse the structural exception handling discussed here with exception handling in C + +. Exception handling in C + + is another form of exception handling, which uses the keyword catch and throw of C + +.
Microsoft first introduced structural exception handling in Visual C + + version 2.0. Structural exception handling consists mainly of two parts: Interrupt processing (termination handling) and exception handling (exception handling).
2, interrupt processing handle (termination handler)
2.1, interrupt processing handle definition
The interrupt handling handle guarantees that, regardless of how the process leaves another program segment-called the guarded body-the program segments within that handle will always be invoked and executed. Microsoft's Visual C + + compiler's interrupt handling handle syntax is
__try {
// Guarded body
.
.
.
}
__finally {
// Termination handler
.
.
.
}
The __try and __finally here outline the two parts of the interrupt handling handle. In the example above, the operating system and the compiler guarantee that the program segments contained within the __finally will always be run, regardless of what happens to the program segments contained within the __try. Whether you call a return, Goto, or longjump,__finally interrupt handle within a __try program section is always invoked. Its process is
1, execute the code before the try program segment
__try {
2, execute the code in the try program segment
}
__finally {
3, execute the code in the Finally program section
}
4, executing the code after the finally program segment