共用方式為


CException 類別

MFC 程式庫中所有例外狀況的基底類別。

語法

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 資料庫例外狀況(也就是說,根據開放式資料庫連線性而產生 MFC 資料庫類別的例外狀況)
COleDispatchException OLE 分派 (自動化) 例外狀況
CUserException 指出找不到資源的例外狀況
CDaoException 資料存取物件例外狀況(也就是 DAO 類別產生的例外狀況條件)
CInternetException 網際網路例外狀況(也就是網際網路類別的例外狀況)。

這些例外狀況是要與 、 THROW_LAST 、、 trycatchand_catchend_catch 宏搭配 THROW 使用。 如需例外狀況的詳細資訊,請參閱 例外狀況處理,或參閱例外狀況處理 (MFC) 一文

若要攔截特定例外狀況,請使用適當的衍生類別。 若要攔截所有類型的例外狀況,請使用 CException ,然後使用 CObject::IsKindOf 來區分 CException 衍生類別。 請注意, CObject::IsKindOf 僅適用于以 IMPLEMENT_DYNAMIC 宏宣告的類別,以便利用動態類型檢查。 您建立的任何 CException 衍生類別也應該使用 IMPLEMENT_DYNAMIC 宏。

您可以呼叫 或 ,向使用者報告例外狀況的詳細資料,方法是呼叫 GetErrorMessageReportError ,這兩個成員函式可搭配任何 CException 衍生類別使用。

如果其中一個宏攔截到例外狀況,就會 CException 自動刪除物件;請勿自行刪除它。 如果使用 關鍵字攔截 catch 到例外狀況,則不會自動刪除。 如需何時刪除例外狀況物件的詳細資訊,請參閱例外狀況處理 (MFC) 一文

繼承階層架構

CObject

CException

需求

標頭:afx.h

CException::CException

這個成員函式會 CException 建構 物件。

explicit CException(BOOL bAutoDelete);

參數

b_AutoDelete
指定 TRUE 是否已在堆積上設定物件的記憶體 CException 。 當呼叫成員函式來刪除例外狀況時 Delete ,這會導致 CException 刪除物件。 CException指定 FALSE 物件是否位於堆疊上,或為全域物件。 在此情況下, CException 呼叫成員函式時 Delete ,將不會刪除物件。

備註

您通常不需要直接呼叫此建構函式。 擲回例外狀況的函式應該建立衍生類別的 CException 實例並呼叫其建構函式,或者應該使用其中一個 MFC throw 函式,例如 AfxThrowFileException 擲回預先定義的型別。 本檔僅供完整性使用。

CException::Delete

此函式會檢查物件是否 CException 在堆積上建立,如果是的話,它會呼叫 delete 物件上的 運算子。

void Delete();

備註

刪除 CException 物件時,請使用 Delete 成員函式來刪除例外狀況。 請勿直接使用 delete 運算子,因為 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 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
指定例外狀況物件沒有錯誤訊息時所要顯示之訊息的資源識別碼(字串資料表專案)。 如果為 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();
}

另請參閱

CObject
階層架構圖表
例外狀況處理