Skip to content

Latest commit

 

History

History
42 lines (25 loc) · 3.08 KB

exceptions-using-mfc-macros-and-cpp-exceptions.md

File metadata and controls

42 lines (25 loc) · 3.08 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: Exceptions: Using MFC Macros and C++ Exceptions
Exceptions: Using MFC Macros and C++ Exceptions
11/04/2016
exception objects [MFC]
memory leaks [MFC], exception object not deleted
exception handling [MFC], MFC
try-catch exception handling [MFC], mixing MFC macros and C++ keywords
exception objects [MFC], deleting
exceptions [MFC], MFC macros vs. C++ keywords
catch blocks [MFC], mixed
exception handling [MFC], mixed-language
nested try blocks [MFC]
catch blocks [MFC], explicitly deleting code in
try-catch exception handling [MFC], nested try blocks
heap corruption [MFC]
nested catch blocks [MFC]
d664a83d-879b-44d4-bdf0-029f0aca69e9

Exceptions: Using MFC Macros and C++ Exceptions

This article discusses considerations for writing code that uses both the MFC exception-handling macros and the C++ exception-handling keywords.

This article covers the following topics:

Mixing Exception Keywords and Macros

You can mix MFC exception macros and C++ exception keywords in the same program. But you cannot mix MFC macros with C++ exception keywords in the same block because the macros delete exception objects automatically when they go out of scope, whereas code using the exception-handling keywords does not. For more information, see the article Exceptions: Catching and Deleting Exceptions.

The main difference between the macros and the keywords is that the macros "automatically" delete a caught exception when the exception goes out of scope. Code using the keywords does not; exceptions caught in a catch block must be explicitly deleted. Mixing macros and C++ exception keywords can cause memory leaks when an exception object is not deleted, or heap corruption when an exception is deleted twice.

The following code, for example, invalidates the exception pointer:

[!code-cppNVC_MFCExceptions#10]

The problem occurs because e is deleted when execution passes out of the "inner" CATCH block. Using the THROW_LAST macro instead of the THROW statement will cause the "outer" CATCH block to receive a valid pointer:

[!code-cppNVC_MFCExceptions#11]

Try Blocks Inside Catch Blocks

You cannot re-throw the current exception from within a try block that is inside a CATCH block. The following example is invalid:

[!code-cppNVC_MFCExceptions#12]

For more information, see Exceptions: Examining Exception Contents.

See also

Exception Handling