Export (0) Print
Expand All
TRY
Expand Minimize

CATCH

Defines a block of code that catches the first exception type thrown in the preceding TRY block.

CATCH(exception_class, exception_object_pointer_name )

Parameters

exception_class
Specifies the exception type to test for. For a list of standard exception classes, see class CException.
exception_object_pointer_name
Specifies a name for an exception-object pointer that will be created by the macro. You can use the pointer name to access the exception object within the CATCH block. This variable is declared for you.

Remarks

The exception-processing code can interrogate the exception object, if appropriate, to get more information about the specific cause of the exception. Invoke the THROW_LAST macro to shift processing to the next outer exception frame. End the TRY block with an END_CATCH macro.

If exception_class is the class CException, then all exception types will be caught. You can use the CObject::IsKindOf member function to determine which specific exception was thrown. A better way to catch several kinds of exceptions is to use sequential AND_CATCH statements, each with a different exception type.

The exception object pointer is created by the macro. You do not need to declare it yourself.

Note   The CATCH block is defined as a C++ scope delineated by braces. If you declare variables in this scope, they are accessible only within that scope. This also applies to exception_object_pointer_name.

For more information on exceptions and the CATCH macro, see the article Exceptions.

Example

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.
   TRY
   {
      pFile = new CFile( _T( "C:\\WINDOWS\\SYSTEM.INI" ),
         CFile::modeRead | CFile::shareDenyNone );
      ULONGLONG dwLength = pFile->GetLength( );
      CString str;
      str.Format( _T( "Your SYSTEM.INI file is %I64u bytes long.") , dwLength );
      AfxMessageBox( str );
   }
   CATCH( CFileException, pEx )
   {
      // Simply show an error message to the user.
      pEx->ReportError();
   }
   AND_CATCH(CMemoryException, pEx)
   {
      // We can't recover from this memory exception, so we'll
      // just terminate the app without any cleanup. Normally, 
      // an application should do everything it possibly can to
      // clean up properly and not call AfxAbort( ).
      AfxAbort( );
   }
   END_CATCH
   // 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 cleanup code needs to test for NULL.
   if ( pFile != NULL )   {
      pFile->Close( );
      delete pFile;
   }

See Also

MFC Macros and Globals | TRY | AND_CATCH | END_CATCH | THROW | THROW_LAST | CATCH_ALL | CException

Show:
© 2014 Microsoft