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.
// 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 "Error.ch"
PROCEDURE Main
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
RETURN
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.
ENDIF
RETURN lReturn
// 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.
PROCEDURE Main
LOCAL aIndexFiles
aIndexFiles := ;
{ ;
{ "custa", "custno" }, ;
{ "custb", "Upper( lastname + firstname )" },;
{ "custc", "zip" } ;
}
DbfNtxUse( "CUSTOMER", aIndexFiles )
RETURN
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)} )
BEGIN SEQUENCE
// A missing index file will raise an error.
OrdListAdd( cIndexFile )
nIndexFile++
RECOVER USING oError
ErrorBlock(bSaveErrorBlock)
// The error handler cares for other errors
IF .NOT. 2 == oError:OsCode
Break( oError )
ENDIF
// Create the index file and restart with first index file
CreateIndexAndReopen( cDbfFile, ;
cIndexKey, ;
cIndexFile )
nIndexFile := 1
END SEQUENCE
ErrorBlock(bSaveErrorBlock)
ENDDO
RETURN
PROCEDURE CreateIndexAndReopen( cDbfFile, cIndexKey, cIndexFile )
USE (cDbfFile) EXCLUSIVE
INDEX ON &cIndexKey ;
TO &cIndexFile
// Reopen SHARED without index files
USE (cDbfFile)
RETURN
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.