The CATCH procedure provides a generalized mechanism for the handling of exceptions and errors within IDL. Calling CATCH establishes an error handler for the current procedure that intercepts all errors that can be handled by IDL, excluding non-fatal warnings such as math errors.
When an error occurs, each active procedure, beginning with the offending procedure and proceeding up the call stack to the main program level, is examined for an error handler. If an error handler is found, control resumes at the statement after the call to CATCH. The index of the error is returned in the argument to CATCH. The !ERROR (or !SYSERROR) and !ERR_STRING (or !SYSERR_STRING) system variables are also set. If no error handlers are found, program execution stops, an error message is issued, and control reverts to the interactive mode. A call to ON_IOERROR in the procedure that causes an I/O error supersedes CATCH, and takes the branch to the label defined by ON_IOERROR.
This mechanism is similar, but not identical to, the
setjmp/longjmp
facilities in C and the
catch/throw
facilities in C++.
Error handling is discussed in more detail in Controlling Errors .
The following procedure illustrates the use of CATCH:
A = FLTARR(10) ; Define variable A.
CATCH, Error_status ; Establish error handler. When errors occur, the index of the error is returned in the variable Error_status.
IF Error_status NE 0 THEN BEGIN ; This statement begins the error handler.
PRINT, 'Error index: ', Error_status
PRINT, 'Error message:', !ERR_STRING
A=FLTARR(12) ; Handle the error by extending A.
HELP, A ; Even though an error occurs in the line above, program execution continues to this point because the event handler extended the definition of A so that the statement can be re-executed.
Running the ABC procedure causes IDL to produce the following output and control returns to the interactive prompt: