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.
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
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