10.11 Branch Statements
Branch statements modify the flow of execution in a method. There are five branch statements:
- A GoTo statement causes execution to transfer to the specified label in the method.
- An Exit statement transfers execution to the next statement after the end of the immediately containing block statement of the specified kind. If the block is the method block, execution is transferred back to the expression that invoked the method. If the Exit statement is not contained within the kind of block specified in the statement, a compile-time error occurs.
- A Stop statement causes a debugger exception to occur.
- An End statement terminates the program. Finalizers are run before shutdown, but the finally blocks of any currently executing Try statements are not executed. This statement may not be used in programs that are not executable (for example, DLLs).
- A Return statement returns execution to the expression that invoked the method. If the method is a subroutine, the statement is equivalent to an Exit Sub statement and no expression may be supplied. If the method is a function, an expression must be supplied that is classified as a value and whose type is implicitly convertible to the return type of the function. This form is equivalent to assigning to the function return local and then executing an Exit Function statement.
BranchStatement ::= GotoStatement | ExitStatement | StopStatement | EndStatement | ReturnStatement GotoStatement ::= GoTo LabelName StatementTerminator ExitStatement ::= Exit ExitKind StatementTerminator ExitKind ::= Do | For | While | Select | Sub | Function | Property | Try StopStatement ::= Stop StatementTerminator EndStatement ::= End StatementTerminator ReturnStatement ::= Return [ Expression ]
10.1 Blocks | 10.1.1 Local Variables and Parameters | 10.2 Local Declaration Statements | 10.3 With Statement | 10.4 SyncLock Statement | 10.5 Event Statements | 10.6 Assignment Statements | 10.7 Invocation Statements | 10.8 Conditional Statements | 10.9 Loop Statements | 10.10 Exception-Handling Statements | 10.12 Array-Handling Statements | Control Flow Summary (Visual Basic Language Reference)