12.2 Catching Exceptions

The following instructions and examples will show you how to catch exceptions.

·To catch exceptions:

1.Use the TRY macro to set up a TRY block. Execute any program statements that might throw an exception within a TRY block.

2.Use the CATCH macro to set up a CATCH block. Place exception handling code in a CATCH block. The code in the CATCH block is executed only if the code within the TRY block throws an exception of the type specified in the CATCH statement.

The following code skeleton shows how TRY and CATCH blocks are normally arranged:

// normal program statements

...

TRY

{

// execute some code that might throw an exception

}

CATCH( CException, e )

{

// handle the exception here

// "e" contains information about the exception

}

END_CATCH

// other normal program statements

...

Note:

Note the END_CATCH macro that marks the end of the CATCH blocks.

The CATCH macro takes an exception type parameter, so you can selectively handle different types of exceptions with sequential CATCH and AND_CATCH blocks as listed below:

TRY

{

// execute some code that might throw an exception

}

CATCH( CMemoryException, e )

{

// handle the out-of-memory exception here

}

AND_CATCH( CFileException, e )

{

// handle the file exceptions here

}

AND_CATCH( CException, e )

{

// handle all other types of exceptions here

}

END_CATCH