ON ERROR Command

Specifies a command that executes when an error occurs.

ON ERROR   [Command]

Parameters

  • Command
    Specifies the Microsoft Visual FoxPro command to execute. After the command executes, program execution resumes on the line immediately following the line that caused the error. However, if the error-handling procedure includes RETRY, the program line that caused the error is executed again.

    If the command specifies a procedure to execute when an error occurs, you can use ERROR( ), MESSAGE( ), LINENO( ), and PROGRAM( ) to pass the error number, the error message, the program line number, and the program name to the procedure. This information can be used to correct the cause of the error.

Remarks

When an error occurs during program execution, Visual FoxPro executes the command you specify with ON ERROR. Typically, ON ERROR uses DO to execute an error-handling procedure.

Use ON ERROR without a command to restore the default Visual FoxPro error handler.

ON ERROR procedures cannot be nested. If ON ERROR is issued within an ON ERROR procedure, the default Visual FoxPro error handler is restored.

Example

ON ERROR DO errhand WITH ;
   ERROR( ), MESSAGE( ), MESSAGE(1), PROGRAM( ), LINENO( )

** The next line should cause an error ** USE nodatabase ON ERROR && restore system error handler PROCEDURE errhand PARAMETER merror, mess, mess1, mprog, mlineno CLEAR ? 'Error number: ' + LTRIM(STR(merror)) ? 'Error message: ' + mess ? 'Line of code with error: ' + mess1 ? 'Line number of error: ' + LTRIM(STR(mlineno)) ? 'Program with error: ' + mprog

See Also

AERROR( ) | COMRETURNERROR( ) | DO | ERROR | ERROR( ) | FUNCTION | LINENO( ) | MESSAGE( ) | PROGRAM( ) | PROCEDURE | RETRY