Dela via


CFileException::GetErrorMessage

Retrieves text that describes an exception.

virtual BOOL GetErrorMessage(
   LPTSTR lpszError,
   UINT nMaxError,
   PUINT pnHelpContext = NULL 
) const;

Parameters

  • [in, out] lpszError
    Pointer to a buffer that receives an error message.

  • [in] nMaxError
    The maximum number of characters the specified buffer can hold. This includes the terminating null character.

  • [in, out] pnHelpContext
    Pointer to an unsigned integer that receives the help context ID. If NULL, no ID is returned.

Return Value

TRUE if the method was successful; otherwise FALSE.

Remarks

If the specified buffer is too small, the error message is truncated.

Example

The following example uses CFileException::GetErrorMessage.

CFile fileInput;
CFileException ex;

// try to open a file for reading. 
// The file will certainly not 
// exist because there are too many explicit 
// directories in the name. 

// if the call to Open() fails, ex will be 
// initialized with exception 
// information.  the call to ex.GetErrorMessage() 
// will retrieve an appropriate message describing 
// the error, and we'll add our own text 
// to make sure the user is perfectly sure what 
// went wrong. 

if (!fileInput.Open(_T("\\Too\\Many\\Bad\\Dirs.DAT"), CFile::modeRead, &ex))
{
   TCHAR   szCause[255];
   CString strFormatted;

   ex.GetErrorMessage(szCause, 255);

   // (in real life, it's probably more 
   // appropriate to read this from 
   //  a string resource so it would be easy to 
   // localize)

   strFormatted = _T("The data file could not be opened because of this error: ");
   strFormatted += szCause;

   AfxMessageBox(strFormatted);
}
else
{
   // the file was opened, so do whatever work 
   // with fileInput 
   // we were planning...

   fileInput.Close();
}

Requirements

Header: afx.h

See Also

Reference

CException Class

Hierarchy Chart

CException::ReportError

Other Resources

CException Members

Change History

Date

History

Reason

October 2008

Updated whole topic.

Customer feedback.