How to Use the ON ERROR Command and the Error EventLast reviewed: April 30, 1996Article ID: Q130526 |
The information in this article applies to:
SUMMARYIn version 2.x of FoxPro, the ON ERROR command allowed the developer to code a routine to handle errors that occurred while an application was running. This functionality is still present in Visual FoxPro. In addition, Visual FoxPro has a new error event. This article demonstrates how to use the ON ERROR command and the error event in a Visual FoxPro application.
MORE INFORMATIONThe error event allows error processing for a specific control, class, or form to be encapsulated for that object. The error event overrides any ON ERROR routine that is in effect. An application can contain ON ERROR commands, error events, or a combination of both.
Step-by-Step ExampleIn the following example, there might be an application-wide ON ERROR routine, an error event for the form itself, and an error event for the employee ID field itself. The application handles payroll processing. A form in the application allows you to input new employees into a table.
oMyObject.ErrorUsing an error event within a class allows you to encapsulate specific error processing within the class. The error event is inherited by any subclasses defined from the parent class. The error event is not inherited or called automatically from any controls or objects defined within the class or form. For example, if an error event is defined for a form and no error events are coded for individual controls on that form, the error event will not be called if an error is encountered in a text box or command button on the form. To call the form's error event, code the following in each control's error event:
ThisForm.ErrorIn this case, an ON ERROR command might be preferable to placing error handling code in the error event of the form itself. Then each control would not need to include code to invoke error processing. When an error event is invoked, Visual FoxPro automatically passes the number of the error that occurred, the name of the method where the error occurred, and the line number of code on which it occurred. This information should be passed to an ON ERROR program as parameters by using the ERROR() and PROGRAM() functions. As an alternative, the AERROR() function can be used within the routine to obtain this information.
|
Additional reference words: 3.00 VFoxWin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |