Structured Exception Handling (C++)
Windows 95, Windows 98, and Windows 2000 (formerly Windows NT) support a robust approach to handling exceptions, called structured exception handling, which involves cooperation of the operating system but also has direct support in the programming language.
An exception is an event that is unexpected or disrupts the ability of the process to proceed normally. Exceptions can be detected by both hardware and software. Hardware exceptions include dividing by zero and overflow of a numeric type. Software exceptions include those you detect and signal to the system by calling the RaiseException function and special situations detected by Windows.
You can write more reliable code with structured exception handling. You can ensure that resources, such as memory blocks and files, are properly closed in the event of unexpected termination. You can also handle specific problems, such as insufficient memory, with concise structured code that does not rely on goto statements or elaborate testing of return codes.
Structured exception handling works with C and C++ source files. However, it is not specifically designed for C++. Although destructors for local objects will be called if you use structured exception handling in a C++ program (if you use /EHsc), you can ensure that your code is more portable by using C++ exception handling. The C++ exception handling mechanism is more flexible, in that it can handle exceptions of any type.
There are two structured exception handling mechanisms:
Exception handlers, which can respond to or dismiss the exception
Termination handlers, which are called when an exception causes termination inside a block of code
These two types of handlers are distinct, yet they are closely related through a process called "unwinding the stack." When an exception occurs, Windows looks for the most recently installed exception handler that is currently active. The handler can do one of three things:
Pass control to other handlers (fail to recognize the exception).
Recognize but dismiss the exception.
Recognize and handle the exception.
The exception handler that recognizes the exception may not be in the function that was running when the exception occurred. In some cases it may be in a function much higher on the stack. The currently running function, as well as all functions on the stack frame, are terminated. During this process, the stack is "unwound": local variables of terminated functions, unless they are static, are cleared from the stack.
As it unwinds the stack, the operating system calls any termination handlers you've written for each function. Use of a termination handler gives you a chance to clean up resources that otherwise would remain open due to abnormal termination. If you've entered a critical section, you can exit in the termination handler. If the program is going to shut down, you can perform other housekeeping tasks such as closing and removing temporary files.
If you have C modules that use structured exception handling, they can be mixed with C++ modules that use C++ exception handling. See Exception Handling Differences.
For more information, see: