Class Error() Foundation

Class function of the Error Class.


The class object generates Error objects using its class method :new().

Error objects are simple objects that contain instance variables and no methods. When a runtime error occurs, an Error object is created and information about the nature of the error is assigned to its instance variables. After the error object is created, it is passed as an argument to the error handler code block most recently defined using the function ErrorBlock(). This code block in turn may pass the error object to an error handling function. The error handling function can then query the instance variables of the error object in order to take appropriate action.

Using the BREAK statement or the Break() function, Error objects can also be passed to the RECOVER statement of a BEGIN SEQUENCE ... END SEQUENCE construct. In the RECOVER section, the error object can be queried for proper local error handling.

Class methods
Creates an instance of the Error class.
Determine actions for error modes.
Retrieve ignored errors.
Instance variables
Contains an array of operator arguments.
Indicates whether or not default error handling is possible.
Indicates whether an operation may be repeated after an error occurs.
Indicates whether a new value may be substituted for one causing an error.
Instance variable for ad-hoc use.
Contains a short error description.
Contains the file name that could not be opened.
Contains the general error number.
Contains a description of the operation in which an error occurred.
Contains the error code returned by the operating system.
Contains a code indicating the severity of the error.
Contains the error code specific to a subsystem.
Contains the name of the subsystem in which the error occurred.
Contains the ID number of the thread in which an error occurred.
Contains the number of times the error has occurred.
Error handling using ErrorBlock()

// The example demonstrates error handling using a 
// user-defined function and the function ErrorBlock(). 
// An attempt is made to open a file which does not exist. 
// As long as the function MyErrorHandler() returns the value .T. 
// the code that attempts to open the file will execute again. 
// As soon as .F. is returned, the program continues and the 
// error is ignored. 

#include "" 

   LOCAL bOldErrorBlock 
   // Save the error block 
   // and replace it with a new one. 
   bOldErrorBlock := ErrorBlock( {|e| MyErrorHandler(e) } ) 

   USE abcdefgh NEW               // Generate a runtime error 

   ErrorBlock(bOldErrorBlock)     // establish prior error block 


FUNCTION MyErrorHandler( oError ) 
   LOCAL lReturn 

   IF oError:canRetry .AND. ;     // Maximum 4 tries 
      oError:tries < 5            // to open file 

      lReturn := .T.              // New try 

   ELSEIF oError:genCode == EG_OPEN .AND. ; 
          oError:subCode == 1001 

      Alert(  "The file;"      + ; 
                oError:Filename + ; 
              ";does not exist!" ) 

      lReturn := .F.  // Execution will continue on the 
                      // next line after the USE command in the above 
                      // function. 

RETURN lReturn 
Error handling using the RECOVER statement

// In this example, the UDF DbfNtxUse() opens a DBF file and 
// its associated index files. The data required to build the index 
// files is stored in an array. Because the index files do not exist, a 
// runtime error occurs. The error block used issues a break(), which 
// passes the runtime error object to the RECOVER statement. In this 
// section, the error can be resolved by building the required index and 
// opening the newly created index file. 

   LOCAL aIndexFiles 

   aIndexFiles := ; 
      { ; 
         { "custa", "custno" }, ; 
         { "custb", "Upper( lastname + firstname )" },; 
         { "custc", "zip" } ; 

   DbfNtxUse( "CUSTOMER", aIndexFiles ) 


PROCEDURE DbfNtxUse( cDbfFile, aIndexFiles ) 
   LOCAL nIndexFile, nIndexFiles, bSaveErrorBlock, oError 
   LOCAL cIndexFile, cIndexKey 

   USE (cDbfFile) NEW                // Open a DBF-file 
   nIndexFile := 1 
   nIndexFiles := Len( aIndexFiles ) 

   DO WHILE nIndexFile <= nIndexFiles 

      cIndexFile := aIndexFiles[nIndexFile,1] 
      cIndexKey  := aIndexFiles[nIndexFile,2] 

      oError := NIL 
      bSaveErrorBlock := ErrorBlock( {|e| Break(e)} ) 

         // A missing index file will raise an error. 
         OrdListAdd( cIndexFile ) 


      RECOVER USING oError 

         // The error handler cares for other errors 
         IF .NOT. 2 == oError:OsCode 
            Break( oError ) 

         // Create the index file and restart with first index file 
         CreateIndexAndReopen( cDbfFile, ; 
                               cIndexKey, ; 
                               cIndexFile ) 

         nIndexFile := 1 




PROCEDURE CreateIndexAndReopen( cDbfFile, cIndexKey, cIndexFile ) 

   USE (cDbfFile) EXCLUSIVE 

   INDEX ON &cIndexKey ; 
         TO &cIndexFile 

   // Reopen SHARED without index files 
   USE (cDbfFile) 


If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.