Exceptions: Throwing Exceptions from Your Own Functions

HomeOverviewHow Do IFAQ

It is possible to use the MFC exception-handling paradigm solely to catch exceptions thrown by functions in MFC or other libraries. In addition to catching exceptions thrown by library code, you can throw exceptions from your own code if you are writing functions that can encounter exceptional conditions.

When an exception is thrown, execution of the current function is stopped and jumps directly to the catch block of the innermost exception frame. The exception mechanism bypasses the normal exit path from a function. Therefore, you must be sure to delete those memory blocks that would be deleted in a normal exit.

To throw an exception

Note   MFC’s default handling of exceptions applies only to pointers to CException objects (and objects of CException-derived classes). The example above bypasses MFC’s exception mechanism.