Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]

Controlling Input/Output Errors


The default action for handling input/output errors is to treat them exactly like regular errors and follow the error handling strategy set by ON_ERROR. You can alter this default by using the ON_IOERROR procedure to specify the label of a statement to which execution should jump if an input/output error occurs. When IDL detects an input/output error and an error-handling statement has been established, control passes directly to the given statement without stopping program execution. In this case, no error messages are printed.

Note that calls to ON_IOERROR made in the procedure that causes an I/O error supersede any error handling mechanisms created with CATCH and the program branches to the label specified by ON_IOERROR.

When writing procedures and functions that are to be used by others, it is good practice to anticipate and handle errors caused by the user. For example, the following procedure segment, which opens a file specified by the user, handles the case of a nonexistent file or read error.

;Define a function to read, and return a 100-element,  
;floating-point array. 
FUNCTION READ_DATA, FILE_NAME 
 
;Declare error label. 
ON_IOERROR, BAD 
 
;Use the GET_LUN keyword to allocate a logical file unit. 
OPENR, UNIT, FILE NAME, /GET_LUN 
 
A = FLTARR(100)     ;Define data array. 
 
READU, UNIT, A      ;Read the data array. 
 
;Clean up and return. 
GOTO, DONE 
 
;Exception label. Print the error message. 
BAD:  PRINT, !ERR_STRING 
 
;Close and free the input/output unit. 
DONE: FREE_LUN, UNIT 
 
;Return the result. This will be undefined if an error occurred. 
RETURN, A 
 
END 

The important things to note in this example are that the FREE_LUN procedure is always called, even in the event of an error, and that this procedure always returns to its caller. It returns an undefined value if an error occurs, causing its caller to encounter the error.


Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]