Executes a specified statement when an error occurs.

Syntax

ON ERROR [<statement>]

<statement>

The statement to execute when an error occurs. ON ERROR without a <statement> option disables any previous ON ERROR <statement>.

Description

Use ON ERROR as a global error handler for unexpected conditions. For localized error handling—that is, for situations where you expect something might fail, like trying to open a file—use TRY...ENDTRY instead. ON ERROR also acts as a global CATCH; if there is no CATCH for a particular class of exception, an error occurs, which can be handled by ON ERROR.

When ON ERROR is active, dBASE Plus doesn't display its default error dialog; it executes the specified <statement>. To execute more than one statement when an error occurs, make the <statement> DO a program file, or call a function or method. In either case, the code that is executed in response to the error is known as the ON ERROR handler.

The ON ERROR handler usually uses the ERROR( ), MESSAGE( ), PROGRAM( ), and LINENO( ) functions to determine what the error is and where it occurred. In most applications, the only safe response to an unexpected condition is to log the error and quit the application. In some cases, you may be able to fix the problem and use the RETRY command to retry the statement that caused the error; or RETURN from the ON ERROR handler, which skips the statement that caused the error and executes the next statement.

While dBASE Plus is executing an ON ERROR statement, that particular ON ERROR <statement> statement is disabled. Thus, if another error occurs during the execution of <statement>, dBASE Plus responds with its default error dialog. You can, however, set another ON ERROR handler inside a routine called with ON ERROR.

SET("ON ERROR") returns the current ON ERROR <statement>.

Avoid using a dBL command recursively with ON ERROR.