5.4.4 Error Handling Statements
Note
This section describes the implementation of MS-VBAL §5.4.4 Error Handling Statements.
Each error-handling statement is listed below with the node the parser produces for it, the instruction kind it lowers to, and the RD-VBAL page that describes its implementation.
| Statement | AST node | Instruction kind | RD-VBAL |
|---|---|---|---|
On Error GoTo <label> |
OnErrorGoToStatementNode | OnErrorGoTo |
RD-VBAL §5.4.4.1 On Error Statement |
On Error GoTo 0, On Error GoTo -1 |
OnErrorGoToStatementNode |
OnErrorDisable |
RD-VBAL §5.4.4.1 On Error Statement |
On Error Resume Next |
OnErrorResumeStatementNode | OnErrorResumeNext |
RD-VBAL §5.4.4.1 On Error Statement |
Bare Resume, Resume 0 |
ResumeStatementNode | ResumeCurrentStatement |
RD-VBAL §5.4.4.2 Resume Statement |
Resume <label> |
ResumeStatementNode |
ResumeLabel |
RD-VBAL §5.4.4.2 Resume Statement |
Resume Next |
ResumeNextStatementNode | ResumeNext |
RD-VBAL §5.4.4.2 Resume Statement |
Error <number> |
ErrorStatementNode | RaiseError |
RD-VBAL §5.4.4.3 Error Statement |
Error interception
On Error and Resume work by interception, not by dedicated per-error branch logic. Every executor dispatch that
can yield an error routes it through ProcedureExecutor.InterceptError, which applies the activation's
error-handling policy. See RD-VBAL §3.5.4 Execution.
Each activation carries a single ErrorHandlerState: the error-handling mode, the handler target, the active error, and the fault-statement offset. The error-handling modes are described in RD-VBAL §5.4.4.1 On Error Statement.
Where an error is located
An error carries the location of what raised it, and that is where it is reported: the line of a run that ends with
it, where the debugger waits, and Erl
(RD-VBAL §6.1.2.7.1.14 Erl). A standard-library member cannot know where it was called from, so an error it raises, such as CInt("x")'s type
mismatch or Err.Raise's error, has no location of its own.
InterceptError gives an error that has no location the location of the statement that faulted, before the error
reaches the session's error state and the debugger. That located error is the one every handler state holds and the
one that propagates to the caller, so the error is waited at once, at its own statement, and not again at the
statement that called the procedure. An error that already has a location keeps it: an operator's error is located at
its own expression. The located error takes the statement's start column.
The Next of a For or For Each loop has no statement of its own, so an error there, such as error 92, is located
at the For it closes.
Unhandled errors
An error that no handler catches propagates out of the activation; see
RD-VBAL §5.4.4.1 On Error Statement. Break mode is entered when an
unhandled run-time error has occurred; see RD-VBAL §2.3.2 Mode / State.
The Err object
VBA source reads and calls the error object at run time (Err.Number, Err.Raise 5): every error that is raised, handled or not, reaches the session's
error state, which is what Err reports (RD-VBAL §6.1.3.2 Err Class).
The Err class is described in RD-VBAL §6.1.3.2 Err Class.
⏮️ RD-VBAL §5.4.3.9 Set Statement | ⏭️ RD-VBAL §5.4.4.1 On Error Statement