ON ERROR statement

Purpose

Specify an error handling routine; enable or disable error trapping.

Syntax

ON ERROR GOTO {label | line_number}

ON ERROR RESUME NEXT

ON ERROR GOTO 0

Remarks

label or line_number identifies the first line of the error trapping routine.  Once error handling has been turned on with this statement, all run-time errors result in a jump to your error handling code.  You must always use the RESUME statement to continue execution once the error has been handled.

To disable error trapping, use ON ERROR GOTO 0 or ON ERROR RESUME NEXT.  You can use this technique if an error occurs for which you have not defined a recovery path; you can also choose to display the contents of ERR or ERRCLEAR at this time.

The default for error trapping is disabled.  If an error occurs while error trapping is disabled, the error code is placed into the ERR system variable, and execution continues.  Errors can still be trapped by checking the value of the ERR or ERRCLEAR variable with IF ERR THEN or SELECT CASE ERR statements.

Error trapping is local to each Sub and Function.  PowerBASIC does not support global error trapping.

Numeric errors such as Divide-by-zero, Overflow and Underflow are not trapped.  Array out-of-bounds and null-pointer trapping are only enabled if #DEBUG ERROR ON is used.

If you're running a program with error trapping turned off, a run-time error may cause a General Protection Fault (GPF).  A GPF cannot be trapped with ON ERROR.

See also

#DEBUG ERROR, ERR, ERRAPI, ERRCLEAR, ERROR, Error OverviewERROR$, Errors and Error TrappingRESUME, Run-time Errors