CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    Delete_exception

    This is defined at STDAFX.h

    Code:
    #define DELETE_EXCEPTION(e) do { e->Delete(); } while (0)

    Can there anyone explain why they need a do-while loop here?

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Delete_exception

    MFC does not rely on auto-delete semantics of the TRY/CATCH macros, therefore those
    macros are mapped to something closer to the native C++ exception handling mechanism
    when building MFC itself."

    The stdafx.h used to build MFC contains its own entirely different definitions for TRY, CATCH,
    and so on; ones that map more or less directly to try/catch (with some ASSERTs thrown in
    for good measure). Since these internal TRY/CATCH implementations don't have the magic
    delete code, DELETE_EXCEPTION is required to manually call e->Delete.
    So there are three possible implementations of TRY/CATCH: the "normal" implementation
    you get without doing anything (with all the magic auto-delete stuff using
    AFX_EXCEPTION_LINK); the "old" backward-compatible setjmp/longjmp implementation you
    get when you #define _AFX_OLD_EXCEPTIONS; and the implementation MFC uses
    internally, which requires DELETE_EXCEPTION to delete the exceptions. If all this makes
    you want to throw up your hands and pull out your hair, I can sympathize. It would be nice
    if the folks in Redmond rationalized their exception handling to map TRY/CATCH directly to
    try/catch, but I don't think it will happen since that might break lots of existing code.
    So what should you do? If you call an MFC function that can throw an exception, you have
    no choice but to try to catch it. If you have old code that already uses TRY/CATCH, it
    probably works fine and you shouldn't do anything. But if you're writing new code, I would
    avoid TRY/CATCH entirely and stick with try/catch. Why? Three reasons: first, try/catch
    produces smaller code than the MFC macros (no hidden local objects with
    constructor/destructor calls); second, CATCH limits you to catching CException-derived
    exceptions; and finally, it just plain looks better to use real C++. TRY/CATCH was always a
    temporary kludge, so the sooner you forget it, the better. The only catch is, if you use
    try/catch, you have to remember to delete your exception object!

    Code:
     try{
       CallSomeMFCFunction();
     } catch( CException*e) {
     // recover
     e->Delete();
     }

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Delete_exception

    Quote Originally Posted by myron
    This is defined at STDAFX.h

    Code:
    #define DELETE_EXCEPTION(e) do { e->Delete(); } while (0)

    Can there anyone explain why they need a do-while loop here?
    It is used to enclose the macro in a block so that a semicolon at the end of the macro invocation doesn't cause a syntax error:
    Code:
    DELETE_EXCEPTION(whatever);
    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured