Exception Handling in MFC
This article explains the exception-handling mechanisms available in MFC. Two mechanisms are available:
C++ exceptions, available in MFC version 3.0 and later
The MFC exception macros, available in MFC versions 1.0 and later
If you're writing a new application using MFC, you should use the C++ mechanism. You can use the macro-based mechanism if your existing application already uses that mechanism extensively.
You can readily convert existing code to use C++ exceptions instead of the MFC exception macros. Advantages of converting your code and guidelines for doing so are described in the article Exceptions: Converting from MFC Exception Macros.
If you have already developed an application using the MFC exception macros, you can continue using these macros in your existing code, while using C++ exceptions in your new code. The article Exceptions: Changes to Exception Macros in Version 3.0 gives guidelines for doing so.
To enable C++ exception handling in your code, select Enable C++ Exceptions on the Code Generation page in the C/C++ folder of the project's Property Pages dialog box, or use the /EHsc compiler option.
This article covers the following topics:
When to Use Exceptions
Three categories of outcomes can occur when a function is called during program execution: normal execution, erroneous execution, or abnormal execution. Each category is described below.
The function may execute normally and return. Some functions return a result code to the caller, which indicates the outcome of the function. The possible result codes are strictly defined for the function and represent the range of possible outcomes of the function. The result code can indicate success or failure or can even indicate a particular type of failure that is within the normal range of expectations. For example, a file-status function can return a code that indicates that the file does not exist. Note that the term "error code" is not used because a result code represents one of many expected outcomes.
The caller makes some mistake in passing arguments to the function or calls the function in an inappropriate context. This situation causes an error, and it should be detected by an assertion during program development. (For more information on assertions, see C/C++ Assertions.)
Abnormal execution includes situations where conditions outside the program's control, such as low memory or I/O errors, are influencing the outcome of the function. Abnormal situations should be handled by catching and throwing exceptions.
Using exceptions is especially appropriate for abnormal execution.
MFC Exception Support
Whether you use the C++ exceptions directly or use the MFC exception macros, you will use CException Class or
CException-derived objects that may be thrown by the framework or by your application.
The following table shows the predefined exceptions provided by MFC.
|CFileException Class||File exception|
|CArchiveException Class||Archive/Serialization exception|
|CNotSupportedException Class||Response to request for unsupported service|
|CResourceException Class||Windows resource allocation exception|
|CDaoException Class||Database exceptions (DAO classes)|
|CDBException Class||Database exceptions (ODBC classes)|
|COleException Class||OLE exceptions|
|COleDispatchException Class||Dispatch (automation) exceptions|
|CUserException Class||Exception that alerts the user with a message box, then throws a generic CException Class|
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. Although these macros are not recommended for new programming, they are still supported for backward compatibility. In programs that already use the macros, you can freely use C++ exceptions as well. During preprocessing, the macros evaluate to the exception handling keywords defined in the MSVC implementation of the C++ language as of Visual C++ version 2.0. You can leave existing exception macros in place while you begin to use C++ exceptions. For information on mixing macros and C++ exception handling and on converting old code to use the new mechanism, see the articles Exceptions: Using MFC Macros and C++ Exceptions and Exceptions: Converting from MFC Exception Macros. The older MFC exception macros, if you still use them, evaluate to C++ exception keywords. See Exceptions: Changes to Exception Macros in Version 3.0. MFC does not directly support Windows NT structured exception handlers (SEH), as discussed in Structured Exception Handling.
Further Reading About Exceptions
The following articles explain using the MFC library for exception handing:
The following articles compare the MFC exception macros with the C++ exception keywords and explain how you can adapt your code: