次の方法で共有


CException::Delete

更新 : 2007 年 11 月

この関数は、CException オブジェクトがヒープ上に作成されたかどうかを調べ、ヒープ上にある場合は、delete 演算子を呼び出してオブジェクトを削除します。

void Delete( );

解説

CException オブジェクトを削除するときは、Delete メンバ関数で例外を削除します。delete 演算子を直接呼び出さないでください。CException オブジェクトがグローバル オブジェクトであったり、スタック上に作成されていることがあるからです。

CException オブジェクトを削除するかどうかは、オブジェクトの構築時に指定できます。詳細については、「CException::CException」を参照してください。

C++ の try-catch 機構を使用した場合だけ、Delete を呼び出す必要があります。MFC のマクロ TRYCATCH を使用すると、これらのマクロによってこの関数が自動的に呼び出されます。

使用例

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 occurrs 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;
}   

必要条件

ヘッダー : afx.h

参照

参照

CException クラス

階層図

その他の技術情報

CException のメンバ