Vs2010-mfc (MFC Common Class: MFC exception handling)

Source: Internet
Author: User
Tags assert throw exception

Transferred from: http://www.jizhuomi.com/software/236.html

The previous section describes the CFile file operation class, this section is mainly about MFC exception handling.

In the chicken Peck Rice C + + Programming starter series The last section of the chicken Peck Rice: C + + Programming Primer Series 50 (Exception handling) in the C + + standard exception handling mechanism, if you have not learned this aspect, you can go to the tutorial to learn about.

MFC exception handling is similar to C + + standard exception handling, except that it uses some macros when throwing and catching exceptions, and also encapsulates exceptions into the CException class and its derived classes. These macros and exception classes are explained below.

MFC Exception macros

MFC provides exception handling macros including try, CATCH, And_catch, End_catch, THROW, Throw_last, and so on, you see the name is not similar to the C + + standard exception handling keywords? They are actually defined on the basis of try, catch, and throw. The following is the definition of these macros in MFC:

C + + code
//Exception macros using try, catch and throw//(for backward compatibility to previous versions of MFC)  #definetry {Afx_exception_link _afxexceptionlink; try {#definecatch (class, E)} catch (class* e) \{ASSERT (e->iskindof (Runtime_class (class))); \ _afxexceptionlink.m_pexception=e; #defineAnd_catch (class, E)} CATCH (class* e) \{ASSERT (e->iskindof (Runtime_class (class))); \ _afxexceptionlink.m_pexception=e; #defineEnd_catch}}#defineThrow (e) throw E#defineThrow_last () (Afxthrowlastcleanup (), throw)//Advanced macros for smaller code#defineCatch_all (E)} catch (cexception* e) \{{ASSERT (e-IsKindOf (Runtime_class (CException))); \ _afxexceptionlink.m_pexception=e; #defineAnd_catch_all (E)} CATCH (cexception* e) \{{ASSERT (e-IsKindOf (Runtime_class (CException))); \ _afxexceptionlink.m_pexception=e; #defineEnd_catch_all}}}#defineEnd_try} catch (cexception* e) \{ASSERT (e-IsKindOf (Runtime_class (CException))); \ _afxexceptionlink.m_pexception= e; } } 

You can see that the definitions of these macros contain the corresponding C + + exception handling keywords, which are implemented by try, catch, and throw in essence.

MFC Exception Classes

MFC encapsulates the handling of exceptions into the exception class--cexception class and its subclasses. In fact, even if we do not use the MFC exception macros but use C + + standard exception handling, it will also use MFC's CException class and its subclasses. MFC exception classes and their meanings are the following table:

MFC exception Classes Meaning
CSimpleException Base class for resource tension anomalies
CInvalidArgException Invalid parameter exception
CMemoryException Not enough memory
CNotSupportedException Responding to requests that do not support services
CArchiveException Archive/Serialize exception
CFileException File exception
CResourceException Windows Resource Allocation exception
COleException OLE exception
CDBException Database exceptions (ODBC Class)
COleDispatchException scheduling (Automation) exceptions
CUserException Warn the user with a message box and then throw a generic CException exception
CDaoException Database exception (DAO Class)
CInternetException Network exceptions

MFC Exception Handling

The form of the try block for MFC exception handling is as follows:

TRY
{   
Compound Statement
}   
CATCH (MFC exception class name, variable name)
{   
Compound Statement
}   
And_catch (MFC exception class name, variable name)
{   
Compound Statement
}   
And_catch (MFC exception class name, variable name)
{   
Compound Statement
}   
......   
End_catch

Description: A pair of curly braces after a try contains a block of code that might throw an exception, catches and handles an exception with a catch clause, captures a pointer to the exception object, and the "MFC exception class name" in the parentheses is the name of the CException class or its subclasses, and the variable name represents " The MFC exception class name is a pointer variable of type, and if the exception type thrown is inconsistent with the catch clause, then all subsequent and_catch clauses are checked in turn, and if the exception type of the clause is consistent with the throw exception type, the exception is captured and handled by the End_. Catch ends the entire try block.

NOTE:MFC exception macros can only capture exceptions that handle CException and its subclass types .

When we use the MFC class, some will automatically throw the exception, of course, we can use afxthrow****** () if necessary to throw the exception, where the ****** and the above MFC exception class list of each exception class corresponding to, For example, a throw file exception can use AfxThrowFileException (), and parameters can be consulted on MSDN.

MFC exception Handling Instances

Here is a simple MFC exception handling code snippet, understand how to use MFC exception handling.

C + + code
try {CFile file (_t (  " c:\\1.txt  " ), Cfile::moderead); //  Constructs a CFile object file, and opens it in read-only mode and throws a CFileException exception if it does not exist   "CATCH (CFileException, e) { if  (e-> M_cause == Cfileexception::filenotfound) { //  If a CFileException exception is caught and the file is not found, the popup dialog box  MessageBox (_t ( " file not found!   "            return  ; }} end_catch 

The meaning of the above code is very simple, is to open a file C:\1.txt, if this file does not exist, then throws the CFileException exception, after the catch clause is captured to determine whether the file is not found, if yes, pop up the prompt dialog box and return.

Modify the above code slightly to demonstrate the use of afxthrow****** () throwing Exceptions:

C + + code
 try {afxthrowfileexception (cfileexception::   FileNotFound);  //  manually throw CFileException exception   "CATCH (CFileException, e) { if  (E->m_caus E == Cfileexception::filenotfound) { //
      MessageBox (_t ("  file not found!   "            return     

When the above code executes, a file exception is caught in the catch clause.

Finally, to remind you, MFC's recommendation is to no longer use MFC exception macros, but directly use C + + standard exception, it is more flexible.

Vs2010-mfc (MFC Common Class: MFC exception handling)

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.