HTBasic Help
×
Menu
Index

ON ERROR

Defines an event branch for trappable errors.
 
 ON ERROR action
 
Usage:
ON ERROR GOTO 2000
ON ERROR CALL Ertrap
ON ERROR RECOVER Test
 
Example:           ON ERROR.BAS
 
Description:
ON ERROR specifies an error handling routine to be called when an error occurs during program execution. The ON ERROR statement must be executed before the error condition occurs. The routine branched to can evaluate the error condition by using the ERRL, ERRLN and ERRN, functions and any other pertinent information to determine the corrective action to take.
 
If another ON ERROR is executed in a different context, the original ON ERROR definition is restored when control returns to the calling context. ON ERROR is canceled by OFF ERROR but is not disabled by DISABLE. A SUBEXIT, SUBEND or RETURN from the defining subprogram also cancels it.
 
When returning from a CALL or GOSUB, execution normally continues with the offending line. If the error handling routine does not correct the cause of the error, the error will occur again, causing an infinite loop. To avoid re-execution of the line, use ERROR SUBEXIT instead of SUBEXIT or ERROR RETURN instead of RETURN.
 
If an error occurs in an error handling routine called with GOSUB or CALL, it is reported to the user and the program is paused. If an error occurs in an error handling routine called with GOTO or RECOVER, an infinite loop can result.
 
If ON ERROR is not used to handle an error, the program is paused and an error message is displayed on the message line. Pressing CONTINUE will re-execute the offending line. Type CONT followed by the line number of the next line to continue execution without re-executing the offending line.
 
More information about ON ERROR can be found under the "Common Information" heading of ON END.
 
See Also: