HOWTO: Implement Generic Error Trapping in FoxPro

ID: Q151321

The information in this article applies to:

SUMMARY

FoxPro provides the ON ERROR command to allow programmers to trap for specific errors that occur within applications. This article describes how to use a generic error trapping routine and shows an example in the context of testing a table for exclusive use.

MORE INFORMATION

If certain FoxPro commands attempt to use a table that is not opened exclusively, these commands generate the error "Exclusive open of file is required." There are several Xbase commands that require the target table be opened exclusively. These commands include INDEX, INSERT [BLANK] (not INSERT SQL), PACK, REINDEX, MODIFY STRUCTURE, ALTER TABLE, and ZAP. The code example below illustrates a simple method of trapping this error and returning a custom message to the user. This basic example can be modified to handle other errors.

While the default FoxPro error contains useful information for a programmer, its appearance may be disconcerting to an end user. Since there is no command or function in FoxPro to report if a table is opened in shared mode by another machine, the user must attempt to open the table exclusively and trap for the error if the procedure is unsuccessful.

The following code example shows how to set up an ON ERROR routine to trap for error "110-Exclusive Open of File is Required", and report a more user- friendly error message. If the code runs on Microsoft Visual FoxPro for Windows, the MESSAGEBOX() function returns the message to the user. If the code executes on FoxPro for Macintosh, MS-DOS, or UNIX, a WAIT WINDOW appears with the programmer's message. This example creates a table named Junk to demonstrate the concept, and then deletes it after trapping the error.

For more information on the options and parameters available with the MESSAGEBOX() function, see the Visual FoxPro Help file.

Sample Code

   CREATE TABLE junk  (fld1 C(20))
      FOR x = 1 to 2
         INSERT INTO junk VALUES ("This is test data")
      ENDFOR

   ON ERROR DO errhand WITH error()  &&  Activates ON ERROR routine

   CLOSE ALL
   USE junk SHARE
   ZAP

   PROCEDURE errhand
   PARAMETERS merror

   IF merror =110
   IF _windows=.t. and ATC("Visual",VERSION())>0
   =MESSAGEBOX("This procedure requires exclusive use of the table. ;
   Please try again ;
   later.",48,"My Custom Messagebox Title")
   ELSE
   WAIT WINDOW "This procedure requires exclusive use of the table. ;
   Try again later."
   ENDIF
   ENDIF

   USE
   DELETE FILE Junk.dbf
   ON ERROR  && Turns off ON ERROR routine

The user can use the MESSAGEBOX() function in FoxPro for Windows 2.x. However, the programmer must use the SET LIBRARY TO command to direct FoxPro to the FoxTools.fll file. Additionally, the user can use the FXALERT() function to display the message on FoxPro for the Macintosh 2.x. For more information about the MESSAGEBOX() and FXALERT()functions in FoxPro 2.x versions, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q124717
   TITLE     : HOWTO: Use the FXALERT() Function in FOXTOOLS.MLB

   ARTICLE-ID: Q105006
   TITLE     : HOWTO: Use the MsgBox() Function in FOXTOOLS.FLL

Additional query words:
Keywords          : kbVFp300 kbVFp500 kbVFp600 kbVFP260 
Issue type        : kbhowto

Last Reviewed: October 23, 1998