The Err object is an intrinsic object with global scope. There is no need to create an instance of it. The Err object contains information about runtime errors and it provides Raise and Clear methods for generating and clearing runtime errors.
The properties of the Err object are set by the generator of an error, either the Caché Basic runtime system in response to an error condition or by the program calling the Raise method.
When a runtime errors occurs, the properties of the Err object are filled with information that uniquely identifies and describes the error condition.
The Err object may be referenced anywhere in the Basic program but will only contain data for the last error.
These properties can contain a numeric or string literal, or an expression that resolves to a literal.
A number that uniquely defines the error code. Numbers from 1 to 512 are reserved by Caché Basic for indication of runtime errors generated by the system. Numbers from 513 and up are reserved for use by the programmer.
A textual description that describes the nature of the error.
A textual description of the source of the error. This may be the name of the program within which the error occurred or it may be the name of an object. If possible, the location within the code where the error occurred might be included.
This clears the error condition and sets the properties of the object to the empty string. The Clear method takes no arguments; the parentheses are optional.
Raise(number [,description [,source]])
The method generates a user-defined exception.
The Raise method has the following arguments:
|number||The error number.|
|description||Optional — A description of the error.|
|source||Optional — A location associated with the error.|
When an exception is generated using the Raise method, the properties of the Err object are first cleared and then set with the corresponding arguments.
The Raise method and the Err object are commonly used with the TRY and CATCH statements.
The following example issues an error using the Err.Raise method. After displaying the error arguments, it uses the Err.Clear method to clear these error argument values:
Main: On Error Goto ErrorHandler Println "before the error" Err.Raise(100,"Deliberate Error","Main line 3") Println "after the error" ' should not print ErrorHandler: Println "Error1: ", Err.Number, " ", Err.Description," ", Err.Source Err.Clear() Println "Error2: ", Err.Number, " ", Err.Description," ", Err.Source
The following example demonstrates how to use the Err object in a function:
Println ErrorTest(1) Println ErrorTest(0) Function ErrorTest(Arg) On Error Goto ErrorHandler return 1/Arg ErrorHandler: Println "Error ", Err.Number, " ", Err.Description," ", Err.Source Err.Clear() return 0 End Function