다음을 통해 공유


Exceptions: Examining Exception Contents

Although a catch block's argument can be of almost any data type, the MFC functions throw exceptions of types derived from the class CException. To catch an exception thrown by an MFC function, then, you write a catch block whose argument is a pointer to a CException object (or an object derived from CException, such as CMemoryException). Depending on the exact type of the exception, you can examine the data members of the exception object to gather information about the specific cause of the exception.

For example, the CFileException type has the m_cause data member, which contains an enumerated type that specifies the cause of the file exception. Some examples of the possible return values are CFileException::fileNotFound and CFileException::readOnly.

The following example shows how to examine the contents of a CFileException. Other exception types can be examined similarly.

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

For more information, see Exceptions: Freeing Objects in Exceptions and Exceptions: Catching and Deleting Exceptions.

See Also

Concepts

Exception Handling in MFC