Partager via


ON ERROR Command

Specifies a valid expression or Visual FoxPro command to execute when an error occurs in code at run time.

ON ERROR [Command]

Parameters

  • [Command]
    Specifies a valid Visual FoxPro command or expression to execute. Typically, ON ERROR uses the DO command to specify an error-handling procedure or program. For more information, see DO Command.

    To restore the Visual FoxPro system error handler, use ON ERROR without an argument.

Remarks

If the error-handling procedure includes the RETRY command, the program executes the line that caused the error instead of continuing on the immediate following line. For more information, see RETRY Command.

You cannot nest ON ERROR commands; calling the ON ERROR command in procedure specified by an ON ERROR procedure restores the Visual FoxPro system error handler.

Tip

If ON ERROR specifies a procedure, you can pass the error number, the error message, the program line number, and the program name to the procedure by including the ERROR( ), MESSAGE( ), LINENO( ), and PROGRAM( ) functions. You can use this information to determined and correct the cause of the error. For more information, see ERROR( ) Function, LINENO( ) Function, MESSAGE( ) Function, and PROGRAM( ) Function.

Example

The following example uses the ON ERROR command with the DO command to specify an error-handling procedure named errHandler. The DO command also uses the ERROR( ), MESSAGE( ), PROGRAM( ), and LINENO( ) functions to pass additional error information to the errHandler procedure. The USE command specifies a table that does not exist and generates an error. The errHandler error handler executes when the error occurs and displays the error information passed to errHandler. ON ERROR without arguments restores the default Visual FoxPro system error handler.

ON ERROR DO errHandler WITH ;
   ERROR( ), MESSAGE( ), MESSAGE(1), PROGRAM( ), LINENO( )
USE nodatabase  
ON ERROR  && Restores system error handler.

PROCEDURE errHandler
   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
ENDPROC

See Also

Concepts

Error Handler Priority

Handling Run-Time Errors

Reference

Debugging and Error-Handling Language

Other Resources

Error Messages

Commands (Visual FoxPro)