Turns on the reporting of error messages by the operating system or GFA-BASIC.
On Error GoTo label
On Error Resume Next
On Error GoTo 0
label:label
On Error is used to install an error trap in a Sub, Function, or Procedure. The error information can be obtained form the Err object. On Error is implemented for compatibility reasons with VB, although the On Error Resume Next is particular useful to trap errors from OLE (Automation) objects. The preferred way in GFA-BASIC 32 of trapping errors is by using Try/Catch.
On Error GoTo label - Enables the error-handling routine that starts at label specified in the required argument. The label argument is any line label or line number. If a run-time error occurs, control branches to the label, making the error handler active. The specified line must be in the same procedure as the On Error statement; otherwise, a compile-time error occurs.
On Error Resume Next - Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues. Use this form rather than On Error GoTo when accessing OLE objects.
On Error GoTo 0 - Disables any enabled error handler in the current procedure.
OnErrorStatementDemo()
Sub OnErrorStatementDemo()
Dim ObjectRef As Object, Msg$
On Error Resume Next ' Defer error trapping.
' Try to start non existent
' object, then test for
' Check for likely Automation errors.
Set ObjectRef = GetObject("MyWord.Basic")
Trace Hex(Err.HResult)
If Err.Number = 46
Msg = "There was an error attempting to open the Automation object!" + _
Err.Description
MsgBox Msg, , "Deferred Error Test"
End If
End Sub
In case of an error On Error Resume Next statement continues to execute the next line as if the line is enclosed in a Try: line : Catch : EndCatch block. In fact, GFA-BASIC 32 generates code like this, although optimized, to support the VB error trap mechanism. The generated code is therefore incremented with 8 bytes for each line in code guarded with On Error Resume Next. This is true until the trap is disabled using On Error Goto 0.
It is advised to use the Try / Catch method of error trapping as much as possible. The resulting code is smaller and it provides a better overview. In addition, a block guarded with On Error Resume Next might easily catch errors originating from a situation that should be handled, not continued.
Additional background information
One of the assembler instructions generated for Try and On Error is the floating-point command fwait to wait for the FPU to complete the current operation. In case of a floating point error an exception is not generated immediately, but instead deferred to the next floating point operation or a fwait. With slow FPUs, fwait leads to a performance decrease, although, fwait always needs some time to execute. In addition, implicitly GFA-BASIC 32 invokes Err.Clear with each Try and On Error Resume Next statement.
{Created by Sjouke Hamstra; Last updated: 20/10/2014 by James Gaite}