Share via


CException::GetErrorMessage

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

Return Value

Nonzero if the function is successful; otherwise 0 if no error message text is available.

Parameters

lpszError

A pointer to a buffer that will receive an error message.

nMaxError

The maximum number of characters the buffer can hold, including the NULL terminator.

pnHelpContext

The address of a UINT that will receive the help context ID. If NULL, no ID will be returned.

Remarks

Call this member function to provide text about an error that has occurred. For example, call GetErrorMessage to retrieve a string describing the error which caused MFC to throw a CFileException when writing to a CFile object.

****NoteGetErrorMessage will not copy more than nMaxError -1 characters to the buffer, and it will always add a trailing null to end the string.  If the buffer is too small, the error message may be truncated.

Example

Here is an example of the use of CException::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("\\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();
   }

CException OverviewClass MembersHierarchy Chart

See Also   CException::ReportError