Delen via


CException Class

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

class AFX_NOVTABLE CException : public CObject

Members

Public Constructors

Name

Description

CException::CException

Constructs a CException object.

Public Methods

Name

Description

CException::Delete

Deletes a CException object.

CException::ReportError

Reports an error message in a message box to the user.

Remarks

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:

CSimpleException

A base class for resource-critical MFC exceptions

CInvalidArgException

Invalid argument exception condition

CMemoryException

Out-of-memory exception

CNotSupportedException

Request for an unsupported operation

CArchiveException

Archive-specific exception

CFileException

File-specific exception

CResourceException

Windows resource not found or not creatable

COleException

OLE exception

CDBException

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

COleDispatchException

OLE dispatch (automation) exception

CUserException

Exception that indicates that a resource could not be found

CDaoException

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

CInternetException

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.

Inheritance Hierarchy

CObject

CException

Requirements

Header: afx.h

See Also

Reference

CObject Class

Hierarchy Chart

Other Resources

Exception Processing

How Do I: Create my Own Custom Exception Classes?