5.4.2.17 Exit Sub Statement
Note
This section describes the implementation of MS-VBAL §5.4.2.17 Exit Sub Statement.
Syntax
| AST node | Instruction kind | Notes |
|---|---|---|
KeywordStatementNode (Token: "Exit Sub") |
ExitProcedure |
Target: the End Sub closer (RD-VBAL §3.5.3 Lowering Block Statements). |
Exit Sub, Exit Function and Exit Property all lower to the
InstructionKind ExitProcedure
(RD-VBAL §3.5.2 Instruction).
Static Semantics
An Exit Sub must be inside the body of a subroutine; in a Function or in a property it is
VBC09332 (ExitSubNotAllowedInFunctionOrProperty). The rule is
ExitStatementStaticSemantics's, applied when the kind of the procedure is known
(RD-VBAL §5.4.2.18 Exit Function Statement).
Runtime Semantics
The executor dispatches the ExitProcedure instruction: it goes on at the End Sub closer, past which the
activation completes. A breakpoint on the End Sub line is hit, and a step from the Exit Sub stops there, before the
procedure returns.
RD-VBA differs from MS-VBA: MS-VBA goes from the Exit Sub straight back to the caller
(RD-VBAL §3.5.4 Execution).
Reaching the end of the instruction list completes the activation "as if execution had reached the end of the
body" (the wording of MS-VBAL §5.4.2.17). This is the same outcome as an explicit Exit
(RD-VBAL §3.5.4 Execution).
MS-VBAL §6.1.3.2.1 has Exit Sub, Exit Function and Exit Property clear Err. MS-VBA clears it as an
activation completes, by an Exit or by reaching the end of the body alike, and only from within an error handler: an
error On Error GoTo caught. An error On Error Resume Next went on past was handled by none, and is still Err when
the procedure returns; so is an error source set with Err.Number (recorded in Excel).
Implementation
RDCore.Runtime.Execution.ProcedureExecutordispatchesExitProcedureinstructions.