Exception Processing

When a program executes, a number of abnormal conditions and errors called "exceptions" can occur. These may include running out of memory, resource allocation errors, and failure to find files.

The Microsoft Foundation Class Library uses an exception-handling scheme that is modeled closely after the one proposed by the ANSI standards committee for C++. An exception handler must be set up before calling a function that may encounter an abnormal situation. If the function encounters an abnormal condition, it throws an exception and control is passed to the exception handler.

Several macros included with the Microsoft Foundation Class Library will set up exception handlers. A number of other global functions help to throw specialized exceptions and terminate programs, if necessary. These macros and global functions fall into the following categories:

  • Exception macros, which structure your exception handler.

  • Exception-throwing functions, which generate exceptions of specific types.

  • Termination functions, which cause program termination.

For examples and more details, see the article Exceptions.

Exception Macros

TRY

Designates a block of code for exception processing.

CATCH

Designates a block of code for catching an exception from the preceding TRY block.

CATCH_ALL

Designates a block of code for catching all exceptions from the preceding TRY block.

AND_CATCH

Designates a block of code for catching additional exception types from the preceding TRY block.

AND_CATCH_ALL

Designates a block of code for catching all other additional exception types thrown in a preceding TRY block.

END_CATCH

Ends the last CATCH or AND_CATCH code block.

END_CATCH_ALL

Ends the last CATCH_ALL code block.

THROW

Throws a specified exception.

THROW_LAST

Throws the currently handled exception to the next outer handler.

Exception-Throwing Functions

AfxThrowArchiveException

Throws an archive exception.

AfxThrowFileException

Throws a file exception.

AfxThrowMemoryException

Throws a memory exception.

AfxThrowNotSupportedException

Throws a not-supported exception.

AfxThrowResourceException

Throws a Windows resource-not-found exception.

AfxThrowUserException

Throws an exception in a user-initiated program action.

MFC provides two exception-throwing functions specifically for OLE exceptions:

OLE Exception Functions

AfxThrowOleDispatchException

Throws an exception within an OLE automation function.

AfxThrowOleException

Throws an OLE exception.

To support database exceptions, the database classes provide two exception classes, CDBException and CDaoException, and global functions to support the exception types:

DAO Exception Functions

AfxThrowDAOException

Throws a CDaoException from your own code.

AfxThrowDBException

Throws a CDBException from your own code.

MFC provides the following termination function:

Termination Functions

AfxAbort

Called to terminate an application when a fatal error occurs.

See Also

Reference

CException Class

Concepts

MFC Macros and Globals