Exception Handling Topics (MFC)

HomeOverviewHow Do IFAQ

This article explains the exception-handling mechanisms available in MFC. Two mechanisms are available:

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.

Note   To enable C++ exception handling in your code, select Enable Exception Handling in the C++ Language category of the C/C++ tab of the Project Settings dialog box, or use the /GX compiler option. The default is /GX–, which disables exception handling.

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.

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 or CException-derived objects that may be thrown by the framework or by your application.

MFC provides several predefined kinds of exceptions:

Exception class Meaning
CMemoryException Out-of-memory
CFileException File exception
CArchiveException Archive/Serialization exception
CNotSupportedException Response to request for unsupported service
CResourceException Windows resource allocation exception
CDaoException Database exceptions (DAO classes)
CDBException Database exceptions (ODBC classes)
COleException OLE exceptions
COleDispatchException Dispatch (automation) exceptions
CUserException Exception that alerts the user with a message box, then throws a generic CException

Note   MFC supports both C++ exceptions and the MFC exception macros. MFC does not directly support Windows NT structured exception handlers (SEH), as discussed in Exception Handling Topics (SEH).

Further Reading About Exceptions

The following articles explain using the class 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: