OnErr Statement
Sets up interrupt branching to cause control to transfer to an error handing subroutine when an error occurs. Allows users to perform error handling.
Syntax
OnErr GoTo {label | 0}
Parameters
- label
- Specify the statement label to jump to in the event of an error.
- 0
- Specify the parameter to clear the OnErr setting.
Description
OnErr enables user error handling. When an error occurs without OnErr being used, the task is terminated and the error is displayed. However, when OnErr is used it allows the user to "catch" the error and go to an error handler to automatically recover from the error. Upon receiving an error, OnErr branches control to the designated label specified in the EResume instruction. In this way the task is not terminated and the user is given the capability to automatically handle the error. This makes work cells run much smoother since potential problems are always handled and recovered from in the same fashion.
When the OnErr command is specified with the “0” parameter, the current OnErr setting is cleared. (i.e. After executing OnErr 0, if an error occurs program execution will stop)
See Also
Err, EResume
OnErr Statement Example
The following example shows a simple utility program which checks whether points P0-P399 exist. If the point does not exist, then a message is printed on the screen to let the user know this point does not exist. The program uses the CX instruction to test each point for whether or not it has been defined. When a point is not defined control is transferred to the error handler and a message is printed on the screen to tell the user which point was undefined.
Function errDemo
Integer i, errNum
OnErr GoTo errHandler
For i = 0 To 399
temp = CX(P(i))
Next i
Exit Function
'
'
'*********************************************
'* Error Handler *
'*********************************************
errHandler:
errNum = Err
' Check if using undefined point
If errNum = 7007 Then
Print "Point number P", i, " is undefined!"
Else
Print "ERROR: Error number ", errNum, " occurred while"
Print " trying to process point P", i, " !"
EndIf
EResume Next
Fend