⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apd.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 3 页
字号:
in with their underlying file and file access functions. CArchiveException has itsown m_cause member to hold archive-specific cause codes, as shown in Table D.2. Youcan raise archive exceptions yourself through the AfxThrowArchiveException() function,which requires a cause code parameter and a lpszArchiveName string pointer for thearchive object throwing the exception.</P><P><H4>TABLE D.2.&nbsp;THE CArchiveException m_cause CODE VALUES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Cause Code		</TD>		<TD ALIGN="LEFT">Meaning		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::none		</TD>		<TD ALIGN="LEFT">No error occurred.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::generic		</TD>		<TD ALIGN="LEFT">The specific cause wasn't specified.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::badSchema		</TD>		<TD ALIGN="LEFT">The wrong version of an object was read.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::badClass		</TD>		<TD ALIGN="LEFT">The class of the object being read was unexpected.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::badIndex		</TD>		<TD ALIGN="LEFT">The file format is invalid.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::readOnly		</TD>		<TD ALIGN="LEFT">Attempt to write on an archive opened for loading.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::writeOnly		</TD>		<TD ALIGN="LEFT">Attempt to read on an archive opened for storing.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CArchiveException::endOfFile		</TD>		<TD ALIGN="LEFT">The end of the file was reached unexpectedly while reading.		</TD>	</TR></TABLE><H3><A NAME="Heading10"></A>Using the Database Exceptions</H3><P>There are two database exception classes: CDBException is used for ODBC-baseddatabase access, and CDAOException is used for DAO-based database access. You canthrow these exceptions yourself with the AfxThrowDBException() function, which needsthree parameters. The first, nRetCode, specifies one of a huge number of databasereturn codes to define the type of error (you should look in the ODBC documentationfor these). The second parameter, pDB, is a pointer to the database associated withthe exception, and the third parameter, hstmt, is an ODBC handle to the SQL statementobject that was executed, causing the exception.</P><P>The RETCODE type is available from the CDBException object via its m_nRetCodemember. You can also access a human-readable piece of error text from the m_strErrormember string and the error text returned from the ODBC driver itself in the m_strStateNativeOriginmember.</P><P>The CDAOException class has a corresponding AfxThrowDaoException() function thatcan throw the DAO exception objects. This function needs just two optional parameters.The first, nAfxDaoError, is a DAO-specific error code that indicates problems withDAO itself (see Table D.3). The second parameter is an OLE SCODE value that is thereturn code from a DAO-based OLE call (see the section &quot;Using OLE Exceptions&quot;for a definition of SCODEs).</P><P><H4>TABLE D.3.&nbsp;DAO COMPONENT-SPECIFIC ERROR CODES FROM nAfxDaoError.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Error Code		</TD>		<TD ALIGN="LEFT">Meaning		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">NO_AFX_DAO_ERROR		</TD>		<TD ALIGN="LEFT">The exception was due to a DAO-specific problem; you should check the supplied CDaoErrorInfo			object and SCODE value.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">AFX_DAO_ERROR_ENGINE_INITIALIZATION		</TD>		<TD ALIGN="LEFT">The Microsoft Jet Engine database engine failed during initialization.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">AFX_DAO_ERROR_DFX_BIND		</TD>		<TD ALIGN="LEFT">A DAO record set field exchange address is invalid.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<PRE>AFX_DAO_ERROR_OBJECT_NOT_OPEN</PRE>		</TD>		<TD ALIGN="LEFT">			<PRE>The queried table hasn't been opened.</PRE>		</TD>	</TR></TABLE></P><P>The CDAOException class has three member attributes: m_scode, which holds an asso-ciatedOLE SCODE value with the attempted operation; or S_OK, if the OLE operation was successful.The m_nAfxDaoError member holds one of the DAO-specific values from Table D.3. Them_pErrorInfo is a pointer to a CDaoErrorInfo structure that holds an error code,descriptive error strings, and a help context ID that is defined like this:</P><P><PRE>struct CDaoErrorInfo{   long m_lErrorCode;   CString m_strSource;   CString m_strDescription;   CString m_strHelpFile;   long m_lHelpContext;};</PRE><P>By interrogating this structure, you can find most of the specific database errordetails pertaining to the DAO exception.</P><P>DAO exceptions can describe more than one error at a time, so you can use theGetErrorCount() member function to find out how many are being referenced. Theseother errors can then be obtained by passing the GetErrorInfo() function a zero-basedindex to the specific error. After calling GetErrorInfo() with a specific index inthe range returned by the GetErrorCount() function, m_pErrorInfo will be updatedto point to the specified object, and thus you can retrieve those values.</P><P><H3><A NAME="Heading11"></A>Using OLE Exceptions</H3><P>There are two types of OLE exceptions, represented by two classes: the COleExceptionclass, which is normally used for server-side or OLE-specific operations, and theCOleDispatchException class, which is used when dealing with client-side IDispatch-basedoperations such as calling ActiveX object functions.</P><P>The simpler of the two is the COleException class, which can be generated by callingthe AfxThrowOleException() function passing an OLE SCODE value. An OLE SCODE is a32-bit error code that is used to represent any kind of error arising from an OLEfunction.</P><P>This value would probably arise from the return code of a function call to a functionon one of the interfaces of an OLE object. This SCODE value will then be stored inthe exception's m_sc member for analysis from within a catch block.</P><P>There is also a Process() static member function that is passed an exception objectand will turn that exception into an SCODE value to represent that exception.</P><P>The COleDispatchException class is used in conjunction with OLE IDispatch interfacesand is thrown by the AfxThrowOleDispatchException() function. This function has twoforms, both with two mandatory parameters and an optional parameter. The first parameterfor both forms is a wCode WORD value that is an application-specific error code.The second parameter is an lpszDescription string pointer in one form, or nDescriptionIDfor a UINT resource code; both types represent either a verbal string or a stringresource code for a verbal string describing the error. The last optional parameteris a help context ID.</P><P>These values are then available as member variables of the COleDispatchExceptionobject via m_wCode, m_strDescription, and m_dwHelpContext. If a help context is specifiedand a help file available, the framework will fill in an m_strHelpFile string identifyingthe help file. The name of the application producing the error can also be soughtfrom the m_strSource attribute.</P><P>If you raise this exception from an OLE object such as an ActiveX control, VisualBasic or any other application using the control or object will display these exceptiondetails.</P><P><H3><A NAME="Heading12"></A>Using the Not Supported Exception</H3><P>The CNotSupportedException class represents exception objects that are generatedwhen an unsupported MFC, operating system, or user-application-specific feature isrequested. If you want to raise this exception, use AfxThrowNotSupportedException(),which doesn't required any parameters. There are also no extended members or functionsassociated with this exception--it just means unsupported.</P><P><H3><A NAME="Heading13"></A>Using the User Exception</H3><P>You can use the CUserException class to generate application-specific exceptionobjects. You might want to do this when your program is interacting with the userto halt the process should she choose a certain option. For example, when you areusing the AppWizard, you can press Esc at any time to cancel the whole process. Microsoftmight have used CUserException to do this by detecting the Esc key and then raisinga user exception object.</P><P>This exception can be raised by a call to the AfxThrowUserException() functionand then caught in the usual try and catch blocks. There are some places in the MFCwhere this exception is raised, such as during dialog box validation or if the fileis too big for an edit view.</P><P><H3><A NAME="Heading14"></A>Generating Your Own Custom Exception Classes</H3><P>You can derive your own exception classes from CException and add your specificextended functionality. Listing D.6 shows the class definition for such a customexception class that extends the normal functionality by adding a m_strMessage CStringvariable to the exception, enabling you to specify your own message when constructingthe exception.</P><P><H4>LISTING D.6.&nbsp;LST29_6.CPP--CLASS DEFINITION FOR CCustomException IMPLEMENTEDIN CustomException.h.</H4><PRE>1:  // ** CustomException.h2:  // ** Header file for CCustomException3: 4:  class CCustomException : public CException5:  {6:      DECLARE_DYNAMIC(CCustomException);7: 8:  public:9:      CCustomException(CString strMessage);10: 11:      CString m_strMessage;12:  };</PRE><P>In Listing D.6 the class is implemented in its own CustomException.h header fileand derives from CException in line 4. The DECLARE_DYNAMIC macro in line 6 suppliesthe MFC CObject-derived runtime class information required for you to decide theexception type in a catch-all catch block. The constructor definition in line 9 takesa CString strMessage parameter to let you create the custom exception with the messagethat will be stored in the m_strMessage CString variable declared in line 11.</P><P>The corresponding CCustomException class implementation is shown in Listing D.7.</P><P><H4>LISTING D.7.&nbsp;LST29_7.CPP--IMPLEMENTATION OF THE CCustomException CLASS.</H4><PRE>1:  // ** CustomException.cpp2:  // ** Implementation for CCustomException exception3: 4:  #include &quot;stdafx.h&quot;5:  #include &quot;CustomException.h&quot;6: 7:  IMPLEMENT_DYNAMIC(CCustomException,CException);8: 9:  CCustomException::CCustomException(CString strMessage)10:      : m_strMessage(strMessage)11:  {12:  }</PRE><P>In Listing D.7 the usual header files are included, and the IMPLEMENT_DYNAMICmacro is used in line 7 to implement the MFC runtime class information functions.The constructor in line 9 takes the strMessage parameters and initializes the m_strMessage<I></I>member variable with this string value in line 10.</P><P>You can then use the custom exception class in your application, as shown in ListingD.8.</P><P><H4>LISTING D.8.&nbsp;LST29_8.CPP--USING THE NEW CCustomException CLASS.</H4><PRE>1: try2: {3:     // ** Something goes wrong4:     CCustomException* pCustomEx = 5:         new CCustomException(&quot;My custom error occured&quot;);6:     throw(pCustomEx);7: }    8: catch(CCustomException* e)9: {10:     // ** Access the extended m_strMessage string11:     AfxMessageBox(e-&gt;m_strMessage);12:     e-&gt;Delete();13: }</PRE><P>In Listing D.8 a new CCustomException object is created with the application-specificerror text in lines 4 and 5 and is thrown in line 6. This is then caught by the catchkeyword in line 8 and the custom information used by the message box in line 11.The exception is then deleted in line 12.</P><P>If you try this, remember that the implementation code must also have an #includefor the CustomException.h header file to retrieve the class definition like this:</P><P><PRE>#include &quot;CustomException.h&quot;</PRE><H1></H1><CENTER><P><HR><A HREF="../apc/apc.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ape/ape.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR></P><P>&copy; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -