CException::GetErrorMessage
更新 : 2007 年 11 月
発生したエラーに関するテキストを提供します。
virtual BOOL GetErrorMessage(
LPTSTR lpszError,
UINT nMaxError,
PUINT pnHelpContext = NULL
);
パラメータ
lpszError
エラー メッセージを受け取るバッファへのポインタ。nMaxError
バッファに保持できる最大文字数。文字列の終わりを示す NULL 文字も含みます。pnHelpContext
ヘルプ コンテキスト ID を受け取る UINT 型変数のアドレス。NULL のときは、ID は返されません。
戻り値
正常終了した場合は 0 以外を返します。それ以外でエラー メッセージ テキストが利用できない場合は 0 を返します。
解説
たとえば、GetErrorMessage を呼び出して、CFile オブジェクトに書き込み中に、MFC が CFileException をスローする原因となったエラーを説明する文字列を取得します。
メモ : |
---|
GetErrorMessage はバッファに nMaxError -1 文字を超える文字列をコピーしません。文字列の末尾には常に NULL 文字を付加します。バッファが小さすぎると、文字列は切り捨てられます。 |
使用例
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(_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