Skip to content

Latest commit

 

History

History
45 lines (27 loc) · 2.72 KB

exceptions-catching-and-deleting-exceptions.md

File metadata and controls

45 lines (27 loc) · 2.72 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: Exceptions: Catching and Deleting Exceptions
Exceptions: Catching and Deleting Exceptions
11/04/2016
exceptions [MFC], deleting
AND_CATCH macro [MFC]
try-catch exception handling [MFC], catching and deleting exceptions
exception handling [MFC], catching and deleting exceptions
catch blocks [MFC], catching and deleting exceptions
execution [MFC], returns from within catch block
7c233ff0-89de-4de0-a68a-9e9cdb164311

Exceptions: Catching and Deleting Exceptions

The following instructions and examples show you how to catch and delete exceptions. For more information on the try, catch, and throw keywords, see Modern C++ best practices for exceptions and error handling.

Your exception handlers must delete exception objects they handle, because failure to delete the exception causes a memory leak whenever that code catches an exception.

Your catch block must delete an exception when:

  • The catch block throws a new exception.

    Of course, you must not delete the exception if you throw the same exception again:

    [!code-cppNVC_MFCExceptions#3]

  • Execution returns from within the catch block.

Note

When deleting a CException, use the Delete member function to delete the exception. Do not use the delete keyword, because it can fail if the exception is not on the heap.

To catch and delete exceptions

  1. Use the try keyword to set up a try block. Execute any program statements that might throw an exception within a try block.

    Use the catch keyword to set up a catch block. Place exception-handling code in a catch block. The code in the catch block is executed only if the code within the try block throws an exception of the type specified in the catch statement.

    The following skeleton shows how try and catch blocks are normally arranged:

    [!code-cppNVC_MFCExceptions#4]

    When an exception is thrown, control passes to the first catch block whose exception-declaration matches the type of the exception. You can selectively handle different types of exceptions with sequential catch blocks as listed below:

    [!code-cppNVC_MFCExceptions#5]

For more information, see Exceptions: Converting from MFC Exception Macros.

See also

Exception Handling