次の方法で共有


Exceptions: Examining Exception Contents

OverviewHow Do IFAQ

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
{
    // Do something to throw a file exception.
}
catch( CFileException* theException )
{
    if( theException->m_cause == CFileException::fileNotFound )
        TRACE( "File not found\n" );
    theException->Delete();
}

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