Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Exit Statement (Visual Basic)

Exits a procedure or block and transfers control immediately to the statement following the procedure call or the block definition.

Exit { Do | For | Function | Property | Select | Sub | Try | While }

Do

Immediately exits the Do loop in which it appears. Execution continues with the statement following the Loop statement. Exit Do can be used only inside a Do loop. When used within nested Do loops, Exit Do exits the innermost loop and transfers control to the next higher level of nesting.

For

Immediately exits the For loop in which it appears. Execution continues with the statement following the Next statement. Exit For can be used only inside a For...Next or For Each...Next loop. When used within nested For loops, Exit For exits the innermost loop and transfers control to the next higher level of nesting.

Function

Immediately exits the Function procedure in which it appears. Execution continues with the statement following the statement that called the Function procedure. Exit Function can be used only inside a Function procedure.

Property

Immediately exits the Property procedure in which it appears. Execution continues with the statement that called the Property procedure, that is, with the statement requesting or setting the property's value. Exit Property can be used only inside a property's Get or Set procedure.

Select

Immediately exits the Select Case block in which it appears. Execution continues with the statement following the End Select statement. Exit Select can be used only inside a Select Case statement.

Sub

Immediately exits the Sub procedure in which it appears. Execution continues with the statement following the statement that called the Sub procedure. Exit Sub can be used only inside a Sub procedure.

Try

Immediately exits the Try or Catch block in which it appears. Execution continues with the Finally block if there is one, or with the statement following the End Try statement otherwise. Exit Try can be used only inside a Try or Catch block, and not inside a Finally block.

While

Immediately exits the While loop in which it appears. Execution continues with the statement following the End While statement. Exit While can be used only inside a While loop. When used within nested While loops, Exit While transfers control to the loop that is one nested level above the loop where Exit While occurs.

Do not confuse Exit statements with End statements. Exit does not define the end of a statement.

The following example uses the Exit statement to exit a For...Next loop, a Do loop, and a Sub procedure.

Sub exitStatementDemo()
    Dim demoNum As Single 
    ' Set up an infinite loop. 
    Do 
        For i As Integer = 1 To 10000000
            demoNum = Int(Rnd() * 100)
            Select Case demoNum
                Case 7 : Exit For 
                Case 29 : Exit Do 
                Case 54 : Exit Sub 
            End Select 
        Next i
    Loop 
End Sub
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft