Table of Contents

5.4.4.1 On Error Statement

Note

This section describes the implementation of MS-VBAL §5.4.4.1 On Error Statement.

Syntax

AST node Instruction kind(s) Notes
OnErrorGoToStatementNode OnErrorGoTo On Error GoTo <label>. Target: the label's offset.
OnErrorGoToStatementNode OnErrorDisable On Error GoTo 0, and the undocumented VBA6/VBA7 form On Error GoTo -1.
OnErrorResumeStatementNode OnErrorResumeNext On Error Resume Next.

See RD-VBAL §3.4.2 Simple Statements and RD-VBAL §3.5.2 Instruction.

Static Semantics

Condition Compile error
On Error GoTo names a handler label that is not defined, e.g. On Error GoTo Handler with no Handler:. VBC09309 — Label not defined

On Error GoTo 0 and On Error GoTo -1 do not refer to a label. The 0 of On Error GoTo 0 and the -1 of On Error GoTo -1 are never looked up as labels: VBC09309 is not raised for them, whether or not the procedure defines a line 0.

Runtime Semantics

An On Error statement sets the error-handling policy of the current activation. The policy is an ErrorHandlingMode:

Policy (ErrorHandlingMode) Set by When an error is raised
Disabled (the policy MS-VBAL §5.4.4 Error Handling Statements calls Default) On Error GoTo 0, On Error GoTo -1; an error caught under GoTo The error is not caught: it propagates out of the activation (see Propagation).
ResumeNext On Error Resume Next The error is caught silently. Execution continues at the statement that follows the one that raised the error. The policy is not reset: every later error in the same activation is caught the same way.
GoTo On Error GoTo <label> The error branches to the label. The policy is reset to disabled: a second, unhandled error inside the handler body propagates rather than re-entering the handler.

The asymmetry between On Error Resume Next (policy kept) and On Error GoTo <label> (policy reset to disabled) follows MS-VBAL.

In MS-VBA, On Error GoTo 0 turns error handling off, and On Error GoTo -1 clears the active error. RD-VBA treats the two identically: both lower as OnErrorDisable, which turns the activation's error handling off and clears any active error. MS-VBAL does not document On Error GoTo -1, so treating it as On Error GoTo 0 does not diverge from the specification.

Error interception

When a statement raises a run-time error:

  1. The executor routes the error through ProcedureExecutor.InterceptError, before the loop decides whether to stop.
  2. InterceptError applies the activation's error-handling policy, as the table above shows.
  3. When the policy does not catch the error, the error propagates out of the activation.

Every runtime error the executor can raise, from any subsystem, is catchable by an error handler: for example TypeMismatch, SubscriptOutOfRange and ForLoopNotInitialized. See RD-VBAL §3.5.4 Execution and RD-VBAL §2.6.3 Runtime Errors.

Propagation

An error that propagates out of an activation (no handler caught it) is returned as the ProcedureExecutor.Run call's own return value.

In a called procedure, that value reaches the caller as the result of the call. A nested call's runtime error propagates like any other runtime error, so the caller's own error-handling policy applies to it. See RD-VBAL §5.3.1.11 Procedure Invocation Argument Processing.

Per-activation state

activation.ErrorHandler, of type ErrorHandlerState, is a single mutable value per activation, like Pc. It holds the error-handling mode, the handler target, the active error, and the fault-statement offset.

The error-handler state is per activation, not per-offset hidden state the way With, Select, For and For Each state is. An On Error statement changes the activation's error-handling policy going forward; it is not scoped to one block.

Implementation

Type or member Role
RDCore.Runtime.Execution.ProcedureExecutor.InterceptError The one point every run-time error passes through; applies the activation's error-handling policy.
ErrorHandlerState The activation's error-handling mode, handler target, active error and fault-statement offset (RDCore.SDK).
ErrorHandlingMode Disabled, ResumeNext, GoTo (RDCore.SDK).
ICallStackFrame.ErrorHandler Reads the activation's ErrorHandlerState; read-only on the SDK interface, and written only by the executor through CallStackFrame.ErrorHandler. See RD-VBAL §3.5.5 Placement and Licensing.
InstructionListLowering Lowers On Error GoTo 0 and On Error GoTo -1 as OnErrorDisable, and On Error GoTo <label> as OnErrorGoTo (RDCore.SDK).

⏮️ RD-VBAL §5.4.4 Error Handling Statements | ⏭️ RD-VBAL §5.4.4.2 Resume Statement