This documentation is archived and is not being maintained.

CException Class

The base class for all exceptions in the Microsoft Foundation Class Library.

class AFX_NOVTABLE CException : public CObject

Because CException is an abstract base class you cannot create CException objects directly; you must create objects of derived classes. If you need to create your own CException-style class, use one of the derived classes listed above as a model. Make sure that your derived class also uses IMPLEMENT_DYNAMIC.

The derived classes and their descriptions are listed below:


A base class for resource-critical MFC exceptions


Invalid argument exception condition


Out-of-memory exception


Request for an unsupported operation


Archive-specific exception


File-specific exception


Windows resource not found or not creatable


OLE exception


Database exception (that is, exception conditions arising for MFC database classes based on Open Database Connectivity)


OLE dispatch (automation) exception


Exception that indicates that a resource could not be found


Data access object exception (that is, exception conditions arising for DAO classes)


Internet exception (that is, exception conditions arising for Internet classes).

These exceptions are intended to be used with the THROW, THROW_LAST, TRY, CATCH, AND_CATCH, and END_CATCH macros. For more information on exceptions, see Exception Processing, or see the article Exception Handling (MFC).

To catch a specific exception, use the appropriate derived class. To catch all types of exceptions, use CException, and then use CObject::IsKindOf to differentiate among CException-derived classes. Note that CObject::IsKindOf works only for classes declared with the IMPLEMENT_DYNAMIC macro, in order to take advantage of dynamic type checking. Any CException-derived class that you create should use the IMPLEMENT_DYNAMIC macro, too.

You can report details about exceptions to the user by calling GetErrorMessage or ReportError, two member functions that work with any of CException's derived classes.

If an exception is caught by one of the macros, the CException object is deleted automatically; do not delete it yourself. If an exception is caught by using a catch keyword, it is not automatically deleted. See the article Exception Handling (MFC) for more information about when to delete an exeption object.

Header: afx.h