Exit 陳述式 (Visual Basic)Exit Statement (Visual Basic)

結束程式或區塊,並立即將控制權轉移至程序呼叫或區塊定義之後的語句。Exits a procedure or block and transfers control immediately to the statement following the procedure call or the block definition.

語法Syntax

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

陳述式Statements

Exit Do
立即結束出現的 Do 迴圈。Immediately exits the Do loop in which it appears. 執行作業會繼續使用 Loop 語句後面的語句。Execution continues with the statement following the Loop statement. Exit Do 只能用在 @no__t 1 迴圈內。Exit Do can be used only inside a Do loop. 在 nested Do 迴圈中使用時,Exit Do 會結束最內層的迴圈,並將控制權轉移至下一個較高層級的嵌套。When used within nested Do loops, Exit Do exits the innermost loop and transfers control to the next higher level of nesting.

Exit For
立即結束出現的 For 迴圈。Immediately exits the For loop in which it appears. 執行作業會繼續使用 Next 語句後面的語句。Execution continues with the statement following the Next statement. Exit For 只能用在 For ... NextFor Each ... Next 迴圈內。Exit For can be used only inside a For...Next or For Each...Next loop. 在 nested For 迴圈中使用時,Exit For 會結束最內層的迴圈,並將控制權轉移至下一個較高層級的嵌套。When used within nested For loops, Exit For exits the innermost loop and transfers control to the next higher level of nesting.

Exit Function
立即結束出現的 Function 程式。Immediately exits the Function procedure in which it appears. 執行會繼續進行呼叫 Function 程式之語句後面的語句。Execution continues with the statement following the statement that called the Function procedure. Exit Function 只能用在 Function 程式內。Exit Function can be used only inside a Function procedure.

若要指定傳回值,您可以在 Exit Function 語句之前的一行上,將值指派給函數名稱。To specify a return value, you can assign the value to the function name on a line before the Exit Function statement. 若要指派傳回值並在一個語句中結束函式,您可以改為使用Return 語句To assign the return value and exit the function in one statement, you can instead use the Return Statement.

Exit Property
立即結束出現的 Property 程式。Immediately exits the Property procedure in which it appears. 執行作業會繼續使用呼叫 Property 程式的語句,也就是要求或設定屬性值的語句。Execution continues with the statement that called the Property procedure, that is, with the statement requesting or setting the property's value. Exit Property 只能用在屬性的 Get 或 @no__t 2 程式內。Exit Property can be used only inside a property's Get or Set procedure.

若要在 Get 程式中指定傳回值,您可以在 @no__t 1 語句之前,將值指派給函數名稱。To specify a return value in a Get procedure, you can assign the value to the function name on a line before the Exit Property statement. 若要指派傳回值,並結束一個語句中的 Get 程式,您可以改用 Return 語句。To assign the return value and exit the Get procedure in one statement, you can instead use the Return statement.

Set 程式中,@no__t 1 語句相當於 @no__t 2 語句。In a Set procedure, the Exit Property statement is equivalent to the Return statement.

Exit Select
立即結束出現的 Select Case 區塊。Immediately exits the Select Case block in which it appears. 執行作業會繼續使用 End Select 語句後面的語句。Execution continues with the statement following the End Select statement. Exit Select 只能用在 Select Case 語句內。Exit Select can be used only inside a Select Case statement.

Exit Sub
立即結束出現的 Sub 程式。Immediately exits the Sub procedure in which it appears. 執行會繼續進行呼叫 Sub 程式之語句後面的語句。Execution continues with the statement following the statement that called the Sub procedure. Exit Sub 只能用在 Sub 程式內。Exit Sub can be used only inside a Sub procedure.

Sub 程式中,@no__t 1 語句相當於 @no__t 2 語句。In a Sub procedure, the Exit Sub statement is equivalent to the Return statement.

Exit Try
立即結束出現的 TryCatch 區塊。Immediately exits the Try or Catch block in which it appears. 如果有 @no__t,則會繼續執行,如果有的話,則會使用 End Try 語句之後的語句。Execution continues with the Finally block if there is one, or with the statement following the End Try statement otherwise. Exit Try 只能用在 Try 或 @no__t 2 區塊內,而不能用在 Finally 區塊內。Exit Try can be used only inside a Try or Catch block, and not inside a Finally block.

Exit While
立即結束出現的 While 迴圈。Immediately exits the While loop in which it appears. 執行作業會繼續使用 End While 語句後面的語句。Execution continues with the statement following the End While statement. Exit While 只能用在 @no__t 1 迴圈內。Exit While can be used only inside a While loop. 在 nested While 迴圈中使用時,Exit While 會將控制權傳輸至迴圈,這是在發生 Exit While 的迴圈上方的一個嵌套層級。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.

備註Remarks

請勿混淆 Exit 語句與 @no__t 1 的語句。Do not confuse Exit statements with End statements. Exit 不會定義語句的結尾。Exit does not define the end of a statement.

範例Example

在下列範例中,當 index 變數大於100時,迴圈條件會停止迴圈。In the following example, the loop condition stops the loop when the index variable is greater than 100. 不過,在迴圈中的 If 語句會導致 @no__t 1 語句在索引變數大於10時停止迴圈。The If statement in the loop, however, causes the Exit Do statement to stop the loop when the index variable is greater than 10.

Dim index As Integer = 0
Do While index <= 100
    If index > 10 Then
        Exit Do
    End If

    Debug.Write(index.ToString & " ")
    index += 1
Loop

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10 

範例Example

下列範例會將傳回值指派給函數名稱 myFunction,然後使用 Exit Function 從函式傳回:The following example assigns the return value to the function name myFunction, and then uses Exit Function to return from the function:

Function MyFunction(ByVal j As Integer) As Double
    MyFunction = 3.87 * j
    Exit Function
End Function

範例Example

下列範例會使用Return 語句來指派傳回值並結束函式:The following example uses the Return Statement to assign the return value and exit the function:

Function MyFunction(ByVal j As Integer) As Double
    Return 3.87 * j
End Function

另請參閱See also