How To Implement Generic Error Trapping in FoxPro

Last reviewed: November 18, 1997
Article ID: Q151321
3.00 3.00b | 2.60a      | 2.60a | 2.60a
WINDOWS    | MACINTOSH  | UNIX  | MS-DOS
kbprg kbhowto

The information in this article applies to:

  • Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b, 5.0
  • Visual FoxPro for Macintosh 3.0a
  • Microsoft FoxPro for Macintosh, version 2.6a
  • Microsoft FoxPro for UNIX, version 2.6
  • Microsoft FoxPro for Windows, version 2.6a
  • Microsoft FoxPro for MS-DOS, version 2.6a

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.

Code Example

   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     : How to Use the FXALERT() Function in FOXTOOLS.MLB

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


Additional reference words: 3.00 3.00b 2.60a FoxDos FoxWin VFoxWin
KBCategory: kbprg kbhowto
KBSubcategory: FxprgGeneral
Keywords : FxprgGeneral kbhowto kbprg
Version : 3.00 3.00b | 2.60a | 2.60a
Platform : MACINTOSH MS-DOS UNIX WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.