12.1 Microsoft Foundation Classes Exception Handling

The Microsoft Foundation Class Library uses an exception-handling scheme that is very similar to one proposed by the ANSI standards committee for C++ 2.1. You set up an exception handler before calling functions that you think might encounter abnormal situations. If your program does run into abnormal conditions, then it throws an exception. When an exception is thrown, program execution jumps to the exception handler and execution resumes there.

Exceptions are represented as objects derived from the abstract class CException. The Microsoft Foundation Class Library provides several predefined kinds of exceptions as listed below.

Exception handler Meaning

CMemoryException Out-of-memory
CFileException File exception
CArchiveException Archive/Serialization exception
CNotSupportedException Response to request for unsupported service
CResourceException Windows resource allocation exception

Since many parts of the Microsoft Foundation Class Library, especially those dealing with files and serialization, use exceptions to report abnormal conditions, you will find it useful to use the Microsoft Foundation exception-handling mechanism in the parts of your program that call those types of Microsoft Foundation Class Library functions. For a description of each Microsoft Foundation Class Library function and the exceptions that can possibly be thrown by that function, see the Class Library Reference. If you see that a function can throw an exception, you should probably surround it with an exception handler.

For a good working example of how to use exceptions with file and serialization functions, see the “Exception Handling” section on page 61 of the tutorial.