You can handle errors that occur in objects at run time by adding error-handling code to the Error event of an object or class definition. If you want an object to have its own error-handling behavior, add error-handling code to the object's Error event. The error-handling behavior you specify applies to that particular object only.
If you want all objects based on a particular class to use the same error-handling behavior, add Error event code to the class definition, for example, for a custom class or subclass. All objects instantiated from the class contain the same error-handling behavior. If the class or subclass does not contain Error event code, it inherits error-handling behavior from the parent class. If Error event code does not exist in the parent class, Visual FoxPro searches for Error event code in another class up the class hierarchy. You can also use the NODEFAULT command and DODEFAULT(В ) function to override or call code from the parent class. For more information, see Overriding and Calling Parent Class Code.
If and when an error occurs in the object at run time, Visual FoxPro searches for Error event code in the object's Error event, the Error event of the base class, or up the class hierarchy for that object, and runs that code, if it exists. Other error handlers might affect how Visual FoxPro handles errors. For more information, see Error Handler Priority.
Error Handling for Objects in Containers
For member objects instantiated within another object or container, for example, controls on a form, you can specify error handling for each member object by adding Error event code for each member object. However, if Error event code does not exist for the member object, it does not inherit Error event code from the container object automatically. If you want the container object's Error event to handle errors for its member objects, you can pass error information from the member object's Error event to container's Error event by using the following code in the member object's Error event:
LPARAMETERS nError, cMethod, nLine THIS.Parent.Error(nError, cMethod, nLine)
You can add code in the container object's Error event to process the error information passed through the parameters specified in this code.
For example, the Vcr class in the Visual FoxPro sample class library, Buttons.vcx, located in the Visual FoxPro ...\Samples\Classes directory, is based on the Visual FoxPro Container class. The container has four command buttons that navigate a table by moving the record pointer.
However, when a user clicks a button when a table is not open, an error can occur. Visual FoxPro attempts to write buffered values to a table when the record pointer moves; therefore, if optimistic row buffering is enabled and another user has changed a value in the buffered record, another error can also occur.
These errors can occur when the user chooses any button; therefore, you do not need to write four separate error-handling routines for each button. Instead, each command button's Error event contains code to pass error information to a single error-handling routine in the Error event for the Vcr class.
You can also include other error handlers, such as the TRY...CATCH...FINALLY and ON ERROR commands, in class definitions and objects. For more information about the order and priority in which error handlers operate, see Error Handler Priority.