CException
クラス
MFC (Microsoft Foundation Class) ライブラリ内のすべての例外に関する基底クラスです。
構文
class AFX_NOVTABLE CException : public CObject
メンバー
パブリック コンストラクター
名前 | 説明 |
---|---|
CException::CException |
CException オブジェクトを構築します。 |
パブリック メソッド
名前 | 説明 |
---|---|
CException::Delete |
オブジェクトを CException 削除します。 |
CException::ReportError |
メッセージ ボックス内のエラー メッセージをユーザーに報告します。 |
解説
抽象基底クラスであるため CException
、オブジェクトを直接作成 CException
することはできません。派生クラスのオブジェクトを作成する必要があります。 独自 CException
のスタイル クラスを作成する必要がある場合は、上記の派生クラスのいずれかをモデルとして使用します。 派生クラスも使用 IMPLEMENT_DYNAMIC
していることを確認します。
派生クラスとその説明を次に示します。
名前 | 説明 |
---|---|
CSimpleException |
リソース クリティカルな MFC 例外の基本クラス |
CInvalidArgException |
引数の例外条件が無効です |
CMemoryException |
メモリ不足の例外 |
CNotSupportedException |
サポートされていない操作の要求 |
CArchiveException |
アーカイブ固有の例外 |
CFileException |
ファイル固有の例外 |
CResourceException |
Windows リソースが見つからないか、作成できない |
COleException |
OLE 例外 |
CDBException |
データベースの例外 (つまり、Open Database Connectivity に基づいて MFC データベース クラスに発生する例外条件) |
COleDispatchException |
OLE ディスパッチ (オートメーション) 例外 |
CUserException |
リソースが見つからなかったことを示す例外 |
CDaoException |
データ アクセス オブジェクトの例外 (つまり、DAO クラスに対して発生する例外条件) |
CInternetException |
インターネット例外 (つまり、インターネット クラスに対して発生する例外条件)。 |
これらの例外は、、、try
、catch
and_catch
およびマクロとend_catch
共にTHROW
THROW_LAST
使用することを目的としています。 例外の詳細については、「例外処理」を参照するか、または例外処理 (MFC) の記事を参照してください。
特定の例外をキャッチするには、適切な派生クラスを使用します。 すべての種類の例外をキャッチするには、CException
-derived クラスをCException
区別するために使用CObject::IsKindOf
し、使用します。 動的な型のCObject::IsKindOf
チェックをIMPLEMENT_DYNAMIC
利用するために、マクロで宣言されたクラスでのみ機能することに注意してください。 作成するすべての CException
派生クラスでも、マクロを使用する IMPLEMENT_DYNAMIC
必要があります。
例外に関する詳細をユーザーに報告するには、いずれかのCException
派生クラスで動作する 2 つのメンバー関数を呼び出すか、または ReportError
2 つのメンバー関数を呼び出GetErrorMessage
します。
マクロの 1 つによって例外がキャッチされた場合、 CException
オブジェクトは自動的に削除されます。自分で削除しないでください。 キーワード (keyword)を使用して例外がcatch
キャッチされた場合、例外は自動的に削除されません。 例外オブジェクトを削除するタイミングの詳細については、例外処理 (MFC) に関する記事を参照してください。
継承階層
CException
必要条件
ヘッダー:afx.h
CException::CException
このメンバー関数は、オブジェクトを CException
構築します。
explicit CException(BOOL bAutoDelete);
パラメーター
b_AutoDelete
オブジェクトのメモリがCException
ヒープに割り当てられているかどうかを指定TRUE
します。 これにより、例外を CException
削除するためにメンバー関数が Delete
呼び出されたときにオブジェクトが削除されます。 オブジェクトがCException
スタック上にあるか、グローバル オブジェクトであるかどうかを指定FALSE
します。 この場合、メンバー関数が CException
呼び出されたときに Delete
オブジェクトは削除されません。
解説
通常、このコンストラクターを直接呼び出す必要はありません。 例外をスローする関数は、-derived クラスの CException
インスタンスを作成してそのコンストラクターを呼び出すか、MFC スロー関数 AfxThrowFileException
のいずれかを使用して定義済みの型をスローする必要があります。 このドキュメントは、完全を期す目的でのみ提供されています。
CException::Delete
この関数チェックオブジェクトがヒープ上に作成されたかどうかをCException
確認し、作成された場合はオブジェクトに対して演算子をdelete
呼び出します。
void Delete();
解説
オブジェクトを削除するときは CException
、メンバー関数を Delete
使用して例外を削除します。 オブジェクトが delete
グローバル オブジェクトであるか、スタック上に作成されている可能性があるため CException
、演算子を直接使用しないでください。
オブジェクトの作成時にオブジェクトを削除するかどうかを指定できます。 詳細については、CException::CException
を参照してください。
C++ try
- catch
メカニズムを使用している場合にのみ呼び出すDelete
必要があります。 MFC マクロを使用している場合、CATCH
これらのマクロTRY
は自動的にこの関数を呼び出します。
例
CFile* pFile = NULL;
// Constructing a CFile object with this override may throw
// a CFile exception, and won't throw any other exceptions.
// Calling CString::Format() may throw a CMemoryException,
// so we have a catch block for such exceptions, too. Any
// other exception types this function throws will be
// routed to the calling function.
// Note that this example performs the same actions as the
// example for CATCH, but uses C++ try/catch syntax instead
// of using the MFC TRY/CATCH macros. This sample must use
// CException::Delete() to delete the exception objects
// before closing the catch block, while the CATCH example
// implicitly performs the deletion via the macros.
try
{
pFile = new CFile(_T("C:\\WINDOWS\\SYSTEM.INI"),
CFile::modeRead | CFile::shareDenyNone);
ULONGLONG ullLength = pFile->GetLength();
CString str;
str.Format(_T("Your SYSTEM.INI file is %u bytes long."), ullLength);
AfxMessageBox(str);
}
catch(CFileException* pEx)
{
// Simply show an error message to the user.
pEx->ReportError();
pEx->Delete();
}
catch(CMemoryException* pEx)
{
// We can't recover from this memory exception, so we'll
// just terminate the app without any cleanup. Normally, an
// an application should do everything it possibly can to
// clean up properly and _not_ call AfxAbort().
pEx->Delete();
AfxAbort();
}
// If an exception occurs in the CFile constructor,
// the language will free the memory allocated by new
// and will not complete the assignment to pFile.
// Thus, our clean-up code needs to test for NULL.
if (pFile != NULL)
{
pFile->Close();
delete pFile;
}
CException::ReportError
このメンバー関数を呼び出して、メッセージ ボックス内のエラー テキストをユーザーに報告します。
virtual int ReportError(
UINT nType = MB_OK,
UINT nMessageID = 0);
パラメーター
nType
メッセージ ボックスのスタイルを指定します。 メッセージ ボックス スタイルの任意の組み合わせをボックスに適用します。 このパラメーターを指定しない場合、既定値は MB_OK
.
nMessageID
例外オブジェクトにエラー メッセージがない場合に表示するメッセージのリソース ID (文字列テーブル エントリ) を指定します。 0 の場合、"エラー メッセージは使用できません" というメッセージが表示されます。
戻り値
AfxMessageBox
値。メッセージ ボックスを表示するのに十分なメモリがない場合は 0。 可能な戻り値については、以下を参照してください AfxMessageBox
。
例
の使用 CException::ReportError
例を次に示します。 別の例については、次の例 CATCH
を参照してください。
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.ReportError() will
// display an appropriate
// error message to the user, such as
// "\Too\Many\Bad\Dirs.DAT contains an
// invalid path." The error message text will be
// appropriate for the
// file name and error condition.
if (!fileInput.Open(_T("\\Too\\Many\\Bad\\Dirs.DAT"), CFile::modeRead, &ex))
{
ex.ReportError();
}
else
{
// the file was opened, so do whatever work
// with fileInput we were planning...
fileInput.Close();
}
関連項目
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示