Error, Error(), LineNo(), Message(), On Error, On("Error"), On Escape, On("Escape"), On ReadError, On("ReadError"), Sys(2018)
These handy functions and commands provide some key debugging facilities to the Visual FoxPro development environment. ERROR(), LINENO(), MESSAGE() and SYS(2018) can provide details about what went wrong, while you can use the ON ERROR, ON ESCAPE and ON READERROR event handlers to control the flow of events and to determine what to do now. We think the ON techniques are mostly relics of bygone Xbase days, but have some suggestions on how to handle errors in Visual FoxPro. ERROR is a cool command that lets you test all the error-handling functions once you have them in place.
Usage |
ERROR nNumber [, cMoreInfo ] | cCustomMessage |
Parameter |
Value |
Meaning |
nNumber |
Integer |
The value of a valid error message—most seem to be in the range of 1 to 1999, although more than a few in that range are still open. Passing a bad error number results (surprise!) in an error—1941, to be precise: "Error code is not valid." Sheesh! |
cMoreInfo |
Character |
If the error message can provide specific information, you can pass that information to the error handler here. This is the equivalent of the information passed back by SYS(2018), and is displayed in the Help file listing as "<name>", as in "File <name> not found." |
cCustomMessage |
Character |
You know those advertisements and billboards that say "Your message could be here?" Well, never to be left out, Visual FoxPro provides this opportunity for you, too, to write error messages. All generate error 1098, the "User-defined error," but with your message. There is no way we can see to supply the equivalent of SYS(2018), however. |
We'd like to be able to use ERROR in an object's ERROR method to invoke the global error handler. Unfortunately, it doesn't work. It calls the default Cancel/Suspend/Ignore dialog. See the entry for the Error method for our thoughts on this one. |
Example |
ERROR 1767, "its child" * Produces the error message "Parent object will not * allow this property setting for its child" - try to say * that with a straight face. ERROR "TILT!" && Generates error 1098 |
Usage |
nError = ERROR() cErrorMessage = MESSAGE() cCode = MESSAGE(1) cMoreInfo = SYS(2018) |
Field validation errors got harder to process after VFP 3. In VFP 3, inserting data into a table that violated the field rule always generated error 1582, and populated the MESSAGE() function with "Field <field name> validation rule is violated" and the SYS(2018) value with the field name. That SYS(2018) value made it easy for us to know which field had the problem, perhaps to set focus back to that control. In VFP 5, SYS(2018) contains the field's RuleText if it's been filled in, or an all-uppercase version of MESSAGE(). There's no easy way to find the field name—you need to parse it out of the MESSAGE() text, which is localized differently for different languages, or scan the DBC for a field with a matching message. They had it right to begin with. Bogus. |
Example |
ON ERROR WAIT WINDOW ; "Error " + LTRIM(STR(ERROR())) + CHR(13)+; "Message() " + MESSAGE() + CHR(13) + ; "Message(1) " + MESSAGE(1) + CHR(13) + ; "SYS(2018) " + SYS(2018) DO LUNCH * File does not exist, and displays: * ERROR() 1 * Message() File 'lunch.prg' does not exist. * Message(1) "do lunch" * SYS(2018) LUNCH.PRG |
Usage |
ON ERROR [ Command ] ON ESCAPE [ Command ] ON READERROR [ Command ] cCommand = ON( "Error" ) cCommand = ON( "Escape" ) cCommand = ON( "ReadError" ) |
Example |
* See the ON ERROR example above. cOldEscape = ON("ESCAPE") && Preserve the old value. lStopReport = .F. ON ESCAPE lStopReport = .T. REPORT FORM MyReport * Within MyReport, call the UDF TestStop(), which follows: * TestStop().PRG * If Escape has been pressed, present an "OK/Cancel" * dialog to the user. If they choose to cancel printing, * force the report to end by jumping to the bottom of the * table. Otherwise, reset the flag to show Escape had been * pressed and resume. IF lStopReport IF MESSAGEBOX("Continue Printing?",17,"Report Paused"); = 2 GO BOTTOM ELSE lStopReport = .F. ENDIF ENDIF RETURN |
See Also |
AError(), DDELastError(), Error Event, FError(), KeyPress, MessageBox(), On Key, Retry, Return, Set Reprocess |