共用方式為


例外狀況:檢查例外狀況內容

catch雖然區塊的引數幾乎可以是任何資料類型,但 MFC 函式會擲回衍生自 類別 CException 的類型例外狀況。 若要攔截 MFC 函式擲回的例外狀況,您可以撰寫區塊 catch ,其引數是物件的指標 CException (或衍生自 CException 的物件,例如 CMemoryException )。 根據例外狀況的確切類型,您可以檢查例外狀況物件的資料成員,以收集例外狀況特定原因的相關資訊。

例如,此 CFileException 類型具有 m_cause 資料成員,其中包含指定檔案例外狀況原因的列舉型別。 可能傳回值的一些範例為 CFileException::fileNotFoundCFileException::readOnly

下列範例示範如何檢查 的內容 CFileException 。 其他例外狀況類型可以類似地檢查。

try
{
   CFile file(_T("\\this_file_should_not_exist.dat"), CFile::modeRead);
}
catch (CFileException* theException)
{
   if (theException->m_cause == CFileException::fileNotFound)
      TRACE("File not found\n");
   theException->Delete();
}

如需詳細資訊,請參閱 例外狀況:釋放例外 狀況和 例外狀況中的物件:攔截和刪除例外狀況

另請參閱

例外狀況處理