HP 3000 Manuals

ON ERROR [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation


HP Business BASIC/XL Reference Manual

ON ERROR 

The ON ERROR statement defines an error-handling routine to handle all
run-time errors that are not trapped by an ON DBERROR or ON END statement
in the same program.

Syntax 

          {GOTO  }
         [{GO TO }        ]
ON ERROR [{GOSUB } line_id]
         [{GO SUB}        ]
         [CALL sub-id     ]

Parameters 

line_id          Line label or line number.  Control will transfer to
                 this line_id when the ON ERROR statement executes.

sub_id           Subunit identifier.  Control will transfer to this
                 subunit when the ON ERROR statement executes.

Table 4-8 shows the similarities and differences between the three forms
of the ON ERROR statement.

          Table 4-8.  ON ERROR STATEMENTS 

----------------------------------------------------------------------------------------------
|                              |                              |                              |
|      Statement Selected      |        Line to Which         |      Scope of ON ERROR       |
|                              |    Control is Transferred    |          Statement           |
|                              |      following ON ERROR      |                              |
|                              |          processing          |                              |
|                              |                              |                              |
----------------------------------------------------------------------------------------------
|                              |                              |                              |
| GOTO line_id                 | None.                        | Program unit that contains   |
|                              |                              | it.                          |
|                              |                              |                              |
----------------------------------------------------------------------------------------------
|                              |                              |                              |
| GOSUB line_id                | Line following the line      | Program unit that contains   |
|                              | where the error occurred.    | it.                          |
|                              |                              |                              |
----------------------------------------------------------------------------------------------
|                              |                              |                              |
| CALL sub_id                  | Line following the line      | Program unit that contains   |
|                              | where the error occurred.    | it and program unit that it  |
|                              |                              | calls, until called unit     |
|                              |                              | executes a local ON ERROR    |
|                              |                              | statement or an OFF ERROR    |
|                              |                              | statement.                   |
|                              |                              |                              |
----------------------------------------------------------------------------------------------

HP Business BASIC/XL provides predefined functions that can be used in
error recovery routines.  They are ERRL, ERRN, ERRM$, and ERRMSHORT$.
They are defined in chapter 5.

Examples 

     100 ON ERROR CALL Default
     110 READ A,B
     120 C=B/A                  !Error can occur here
     130 DISP A,B,C
     135 END
     140 SUB Default
     150   C=0
     160 SUBEND

The next three examples show how the three forms of the ON ERROR
statement transfer control when errors occur.

     100 ON ERROR GOTO 140
     110 I=J/0               !Error occurs; go to line 140.
     120 PRINT "DONE"        !This statement is never executed.
     130 GOTO 999
     140 PRINT "ERROR"       !Execute line 999 next.
     999 END

     100 ON ERROR GOSUB 140
     110 I=J/0               !Error occurs; gosub line 140.
     120 PRINT "DONE"
     130 GOTO 999
     140 PRINT "ERROR"
     150 RETURN              !Return to line 120.
     999 END
     100 ON ERROR CALL Error
     110 I=J/0               !Error occurs; call to line 140.
     120 PRINT "DONE"
     130 END
     135 SUB Error
     140   PRINT "ERROR"
     150 SUBEND              !Return to line 120.

The next three examples show the scope of the three forms of the ON ERROR
statement.

     100 ON ERROR GOTO 115
     105 A=B/0                !Error occurs; go to line 115
     115 PRINT "ERROR"
     116 CALL Sub1
     120 END
     130 SUB Sub1             !ON ERROR at line 100 inactive within Sub1
     140   I=J/0              !Error aborts program
     150 SUBEND

     100 ON ERROR GOSUB 115
     105 A=B/0                !Error occurs; gosub line 115.
     110 CALL Sub1
     115 PRINT "ERROR"
     116 RETURN               !Return to line 110.
     120 END
     130 SUB Sub1             !ON ERROR at line 100 inactive in Sub1
     140   I=J/0              !Error aborts program.
     150 SUBEND

     100 ON ERROR CALL Error
     110 A=B/0                !Error occurs; call Error; return will be to
     115                      !line 115
     120 CALL Sub1
     130 END
     141 SUB Error
     150   PRINT "ERROR"
     160 SUBEND
     170 SUB Sub1             !ON ERROR still active within Sub1
     180   I=J/0              !Error occurs; call Error
     190 SUBEND

The next example shows how a local ON ERROR statement overrides an active
ON ERROR statement in the calling program unit.

     100 ON ERROR CALL Error
     105 P=Q/0                        !Error occurs; call Error
     110 CALL Sub1
     115 R=S/0                        !Error occurs; call Error
     120 CALL Sub2
     125 T=U/0                        !Error occurs; call Error
     130 END
     140 SUB Sub1
     150   A=B/0                      !Error occurs; call Error
     160 SUBEND
     170 SUB Sub2
     175   M=N/0                      !Error occurs; call Error
     180   ON ERROR GOSUB 240         !Overrides line 100
     190   I=J/0                      !Error occurs; GOSUB 210
     200   GOTO 230
     210   PRINT "Error at line 190"
     220   RETURN
     230 SUBEND
     240 SUB Error
     250   PRINT "Error at line 105,115,125,150, or 175"
     260 SUBEND



MPE/iX 5.0 Documentation