共用方式為


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 類別
階層架構圖表
例外狀況處理