Exception Handling Mechanisms

Microsoft C/C++ provides two different forms of support for handling anomalous situations, known as “exceptions,” which may occur during the execution of your program. These two forms are C++ exception handling and structured exception handling .

C++ Exception Handling

C++ exception handling uses three statements added to the C++ language. These are the try, catch, and throw statements. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. These exceptions are handled by code that is outside the normal flow of control. The Microsoft C++ compiler implements the C++ exception handling model based on the ISO WG21/ANSI X3J16 working papers towards the evolving standard for C++.

Structured Exception Handling

Structured exception handling is an extension to Microsoft C/C++ that can be used in either C or C++. Structured exception handling uses two constructs: try-except, also known as exception handling, and try-finally, also known as termination handling. The try-except statement enables applications to gain control of a program when events that normally terminate execution occur. The try-finally statement enables applications to guarantee execution of cleanup code when execution of a block of code is interrupted.

Note   Structured exception handling works with Win32 for both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the new C++ exception-handling mechanism (try, catch, and throw statements).

For more general discussion, see Exception Handling: Overview.