CFileException::GetErrorMessage

检索介绍异常的文本。

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

参数

  • [in,out] lpszError
    对收到错误消息的缓冲区的指针。

  • [in] nMaxError
    指定缓冲区可以容纳的最大字符数。 这包括终止null字符)。

  • [in,out] pnHelpContext
    用于接收帮助上下文ID.的无符号整数的指针 如果 NULL,没有ID返回。

返回值

TRUE,如果方法成功;否则 FALSE。

备注

如果指定的缓冲区太小,错误消息被截断。

示例

下面的示例使用 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();
}

要求

标头: afx.h

请参见

参考

CException Class

层次结构图

CException::ReportError