Sunday 20 November 2011

What is the difference between “exception handling” and “structured exception handling”? in C programming

What is the difference between “exception handling” and “structured exception handling”?

Generally speaking, the difference between a structured exception and exception handling is Microsoft’s implementation of exception handlers themselves. So-called “ordinary” C++ exception handling uses three statements added to the C++ language: try, catch, and throw. The purpose of these statements is to allow a piece of software (the exception handler) to attempt a safe bailout of the application that was running when the exception occurred. The exception handler can trap exceptions on any data type, including a C++ class. The implementation of the three statements is based on the ISO WG21/ANSI X3J16 C++ standard for exception handling. Microsoft C++ supports exception handling based on this standard. Note that this standard applies only to C++ and not to C.

On the other hand, structured exception handling is an extension to the Microsoft C/C++ compiler. Its single largest advantage is that it works with either C or C++. Microsoft’s structured exception handling design uses
two new constructs: try-except and try-finally. These two constructs are not a subset or superset of the ANSI C++ standard; instead, they are a different implementation of exception handling (leave it to Microsoftto forge ahead on its own). The try-except construct is known as exception handling, and try-finally is  known as termination handling. The try-except statement allows an application to retrieve the state of the machine when the exception occurred. This is very handy for displaying information about the error to the user, or for use while you are debugging your code. The try-finally statement enables applications to guarantee execution of cleanup code when normal code execution is interrupted. Although structured exception handling has its advantages, it also has its drawbacks. Because this is not an ANSI standard, code using structured exception handling is not as portable as code using ANSI exception handling. A good rule of thumb is if your application is going to be a C++ program, you are advised to stick to ANSI exception handling (use the try, catch, and throw statements).

Cross Reference:

None.

No comments:

Post a Comment