Partager via


Handling Run-Time Errors

After an application starts executing, errors can occur at run time. For example, errors can occur at run time from the following actions:

  • Writing to a file that does not exist.

  • Attempting to open an open table or select a closed table.

  • Encountering a data conflict.

  • Dividing a value by zero.

You can handle such errors, including errors that occur in reports and those due to tables with bad database back links, by using the error handlers that Visual FoxPro provides. However, if your application does not contain error-handling routines when an error occurs at run time, the application pauses, and the appropriate Visual FoxPro system error message appears with the following buttons:

  • Cancel   If a user chooses Cancel, the application immediately stops running and returns control to the system.

  • Ignore   If a user chooses Ignore, Visual FoxPro ignores the line that caused the error and continues to the next line in the program.

For a list and explanation of Visual FoxPro error messages, see Error Messages.

Tip

For errors that you cannot avoid, make sure to provide documentation that describes errors that users might see and suggest ways to correct those errors.

Using Error Handling Routines

You can use the Visual FoxPro language to create error-handling routines for error conditions. The following topics explain several mechanisms for handling run-time errors in code and the order of their operation:

Running the Most Recently Executed Line of Code

You can use the RETRY command, except with TRY...CATCH...FINALLY, to run a line of code that caused an error after changing conditions that caused the error.

Note

Sometimes, the line of code that caused an error does not exist for you to execute with RETRY. The Error event can occur even when the error encountered is not associated with a line of your code. For example, if you call a data environment's CloseTables method in code when the AutoCloseTables property is set to True (.T.) and then release the form, Visual FoxPro generates an internal error when it attempts to close the tables again. You can trap the error; however, no line of code exists for you to execute with RETRY.

For more information, see RETRY Command.

See Also

Concepts

Handling Errors During Report Runs

Reference

Debugging and Error-Handling Language

Other Resources

Testing and Debugging Applications