Caché Basic Reference
On Error Goto
[Back] [Next]
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Enables an error-handling routine and specifies the location of the routine within a procedure.
Synopsis
On Error GoTo [ label | 0 ]
Arguments
The On Error GoTo statement has the following argument:
label A line label specifying the target of the Goto operation. A label is a valid identifier, followed by a colon suffix. See Labels in Using Caché Basic.
Description
Enables the error-handling routine that starts at the line specified by the label argument. If a runtime error occurs, control branches to the specified line, making the error handler active. The specified line must be in the same procedure as the On Error statement, or a compile-time error will occur.
The label argument specifies an existing label in the current program. Specifying the label's colon suffix is optional. Label names are case-sensitive. Specifying a non-existent label name results in a runtime error.
Use On Error Goto 0 to disable error handling if you have previously enabled it.
When On Error Goto is triggered by an error, it is automatically disabled. This means that the occurrence of a second error causes a program abort, rather than initiating an infinite loop.
An error-handling routine is not a Sub procedure or a Function procedure. It is a section of code marked by a line label.
Error-handling routines rely on the value in the Number property of the Err object to determine the cause of the error. The routine should test or save relevant property values in the Err object before any other error can occur or before a procedure that might cause an error is called. The property values in the Err object reflect only the most recent error. The error message associated with Err.Number is contained in Err.Description.
Examples
The following example shows the use of the On Error Goto statement. Here the error is attempting to divide 6 by 0. The ErrMod error handler displays the error number (18) and description:
Mod1:
  On Error Goto ErrMod
  PrintLn "Mod1 pre-div"
  Println "result: ",6/0
  Println "Mod1 post-div"
  Goto Done
ErrMod:
  Println "Handling an error!"
  PrintLn "Error ",Err.Number," ",Err.Description
Done:
  Println "All done"
 
In the following example, the ErrMod error handler corrects the division by zero problem by changing divisor to 1, and retries the Mod1 operation. Note that invoking the ErrMod error handling module resets On Error Goto, so that the occurrence of the second error in this program (attempting to divide 5 by 0) aborts the program, rather than calling ErrMod again:
Setup:
  On Error Goto ErrMod
  divisor=0
Mod1:
  PrintLn "Mod1 pre-div"
  Println "result: ",6/divisor
  Println "Mod1 post-div"
  Println 5/0
  Goto Done
ErrMod:
  Println "Handling an error!"
  PrintLn "Error ",Err.Number," ",Err.Description
  If Err.Number=18 Then
     divisor=1
     Goto Mod1
  Else
     Println Err.Number
  End If
Done:
  Println "All done"
 
The following example shows the use of the On Error GoTo statement in a user-defined function:
PrintLn ErrorTest(1)
PrintLn ErrorTest(0)
 
Function ErrorTest(Arg)
  On Error Goto ErrDisplay
  return 1/Arg
ErrDisplay:
  PrintLn "Error ", Err.Number, " ", Err.Description, " ", Err.Source
  Err.Clear
  Return 0
End Function
See Also