Visual C++ supports three types of exception handling: C++ exceptions, structured exception handling (SEH), and MFC exceptions. C++ exceptions use try, throw, and catch statements to handle exceptions outside normal program flow. MFC exceptions are derived from the CException class and are thrown by MFC functions. Exception handling identifies exceptions, searches for matching handlers, and executes handler code if a match is found or calls terminate if not.
2. Exceptions Exceptions occur when a program executes abnormally because of conditions outside the program's control. Exception can be defined as an abnormal event that occurs during the execution of a program and disrupts the normal flow of instructions. The abnormal event can also be an error in the program. Certain operations, including object creation and file input/output, are subject to failures that go beyond errors. Out-of-memory conditions, for example, can occur even when your program is running correctly.
3. Use of Exceptions The outcome of a function call during a program execution can be categorized as Normal Execution Occurs when a function executes normally and returns to a calling program. Some functions return a result code to the caller and indicate success or failure (even a particular type of failure). Erroneous Execution Occurs when the caller makes a mistake in passing arguments or calls a function out of context. The error which occurs can be detected by an assertion.
4. Use of Exceptions(contd..) Abnormal Execution Includes situations where conditions outside the program’s control, influence the outcome of a function. For eg. Insufficient memory, abnormality in I/O operations-(while opening a file which exists, the device driver for the storage media might fail.)
5. Exception Handling Visual C++ supports three kinds of exception handling: C++ Exception Handling Although structured exception handling works with C and C++ source files, it is not specifically designed for C++. For C++ programs, C++ exception handling should be used. Structured Exception Handling Windows supplies its own exception mechanism, called SEH. It is not recommended for C++ or MFC programming. SEH should be used only in non-MFC CÂ programs. MFC Exceptions Since version 3.0, MFC has used C++ exceptions but still supports its older exception handling macros, which are similar to C++ exceptions in form.
6. Exception Handling(contd..) The try , throw , and catch statements implement exception handling. 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 C++ Exception Handling mechanism is flexible, since it can handle exceptions of any type. The process of raising an exception is called Throwing an exception. The C++ exception–handling model is non-reusable. Once the flow of program control has left the try block, it never returns to that block.
7. Process of Exception Handling The steps involved in the process of handling exceptions : Control reaches the try statement by normal sequential execution. The guarded section (within the try block) is executed. If exception is not thrown during the execution of the guarded section, the catch clauses that follow the try block are not executed. The statements after the catch block are executed. If an exception is thrown during the execution of the guarded section , a catch clause that can handle the exception is searched. If a matching handler is not found, the predefined run-time function terminate is called. If a matching catch is found, the parameters are initialized, and the instructions in the catch handler are executed.If the program terminates, all the values in the stack are released.
8. Exception Handling in MFC MFC provides several predefined exception classes that are derived from CException class. MFC functions throw exceptions of types derived from the CException class. To catch an exception thrown by a MFC function, a catch block with an argument that is a pointer to a CException object needs to be created.
9. Exception Handling in MFC(contd..) Some of the MFC Exception classes are: Exception Class Functionality CMemoryException Out-of-memory exception CFileException File-specific exception CArchiveException Archive/Serialization exception CDBException Database exceptions CResourceException Windows resource allocation exception