Error Handling


Errors are caught by enclosing the statements to be checked for errors in an iferr block or an ifnoerr block. An iferr block has one of two forms. The first form can only check a single statement and the statement must either be an assignment statement or a procedure call. The second form can check any number of statements of any type. The two forms of the iferr block have the following syntax:

iferr (statement) { 	                   iferr {
   statements                             statements 	
} else {                               } then {
   statements                             statements
}                                      } then {

Figure 3.2: Syntax for iferr.

The else portion of the iferr block is optional. The meaning of an iferr block is that if an error occurs (i.e., if error() was called by one of the statements in the block) while executing the statements checked by the block, then execute the following code, but otherwise execute the code in the else block. The normal action of the error procedure, which is to print a message on the standard error stream, is suppressed. The syntax of an ifnoerr block is the same as that of the iferr block, except that the keyword iferr is replaced by ifnoerr. The meaning of an ifnoerr block is the opposite of that of the iferr block. If no error occurs during the execution of an ifnoerr block, then the following code is executed, otherwise the else block is executed. The following example shows the two forms of the iferr block.

If there is more than one procedure call in a given block, then errchk() all of them except the last (see below).

Figure 3.2: - Syntax for iferr.

Generated with CERN WebMaker