[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


5.2.4 Catching a Particular Exception

The exception scope can express interest in any number of specific exceptions by naming them in CATCH expressions. When an exception reaches the exception scope, control is transferred to the first CATCH clause in the block that matches the exception. If there is more than one CATCH for a given exception within the scope of a single TRY/ENDTRY scope, then only the first one matching the current exception gains control.

To catch an address exception, the CATCH macro must specify the name of the exception object as used in a RAISE macro. However, status exceptions can be caught using any exception object that has been set to the same status code as the exception that was raised. In general, you should RAISE and CATCH using the same exception object even when using status exceptions.

Following is an example of catching a particular exception and specifying the recovery action (in this case, a message). After catching the exception and executing the recovery action, the exception is explicitly reraised (causing it to propagate to its callers):

TRY {
   read_tape ();
}
CATCH (parity_error) {
   printf ("Oops, parity error, program terminating\n");
   printf ("Try cleaning the heads!\n");
   RERAISE;
}
ENDTRY