CException

MFC Library Reference
CException::GetErrorMessage

 

Call this member function to provide text about an error that has occurred.

virtual BOOL GetErrorMessage(
LPTSTR lpszError,
UINT nMaxError,
PUINT pnHelpContext = NULL
);
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.

CException::GetErrorMessage - lgy-047 - lgy-047的博客  Return Value

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

CException::GetErrorMessage - lgy-047 - lgy-047的博客  Remarks

For example, call GetErrorMessage to retrieve a string describing the error which caused MFC to throw a CFileException when writing to a CFile object.

NoteNote

GetErrorMessage 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.

CException::GetErrorMessage - lgy-047 - lgy-047的博客  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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值