异常:检查异常内容

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();
}

有关详细信息,请参阅异常:释放异常和异常中的对象:捕获和删除异常

另请参阅

异常处理