Decrypting exception handling in MFC

Source: Internet
Author: User

There are two kinds of exception handling mechanisms in MFC:

    • C + + exceptions, available in MFC3.0 or later versions
    • MFC exception macros, available in MFC1.0 or later versions


If you are using MFC to write a new application, you should use the C + + exception mechanism, and if your existing application is near using MFC exception macros, you can continue to use MFC exception macros. Of course, you can also replace the existing MFC exception macros with C + + exceptions.


Using C + + exceptions instead of MFC exception Macros benefits:

    • Using C + + exceptions, the code that is written generates a smaller module (exe,dll)
    • The C + + exception keyword is very generic, it can handle any exception type (Int,float,char, etc.), and MFC exception macros can only handle CException classes and classes inherited from CException

The biggest difference between MFC exception Macros and C + + exceptions is that when an exception is caught, the MFC exception macro automatically deletes the caught exception, and the C + + exception requires you to delete the caught exception manually.

    • MFC Exception Macros

try{//Execute Some code that might the throw an exception. Afxthrowuserexception ();} CATCH (CException, E) {//Handle the exception Here.if (M_bthrowexceptionagain) throw_last ();//No need to delete E.} End_catch


    • C + + Exceptions

try{//Execute Some code that might the throw an exception. Afxthrowuserexception ();} catch (cexception* e) {//Handle the exception here.//"E" contains information about the Exception.if (M_bthrowexceptiona Gain) throw; Do not delete Eelse e->delete ();//delete E, no side causes memory leak}


MFC exception macros,TRY, CATCH, and_catch,end_catch, THROW,throw_last; C + + exception keyword, try,catch,throw; Replace MFC exception Macros with C + + exceptions, with the following substitutions:

TRY (Replace it with try)

CATCH (Replace it with catch)

And_catch (Replace it with catch)

End_catch (Delete it)

THROW (Replace it with throw)

Throw_last (Replace it with throw)



P.S The above content is written by reference MSDN2008.


Decrypting exception handling in MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.