Use an Exit statement to jump out of any loop, Select...Case statement, or Try...Catch statement, as well a Function, Method, or Rule. An Exit statement must be contained within the kind of block specified in the statement: Exit For must appear within a For loop, Exit While in a while loop, and so on.
In the following example, when the value of the load parameter exceeds the value of the limit, the While loop is exited and control passes to the End While statement.
Parameter Rule unitWeight As Integer = 5 Rule load As Number Dim counter As Integer = 1 Dim limit As Number = 25.0 While counter <= 10 load = load + unitWeight If load > limit Then printValue("Warning: “ + stringValue(limit) + "kg limit exceeded!") Exit While End If counter = counter + 1 End While End Rule
A similar technique is used to exit the Do and For loop constructs:
‘Exit from a Do loop. Exit Do ‘Exit from a For loop. Exit For
An Exit statement can be used to escape from a Select...Case statement. In the example below, the Select...Case statement is exited if the select expression equals one of the values in the first case clause.
Select nTeeth ‘exit if number is within range, 18-25 Case 18, 21, 25 Exit Select Case 15 description = "15 Tooth Sprocket (custom)" printValue("description = 15 Tooth Sprocket (custom)") Case 28 description = "28 Tooth Sprocket (custom)" printValue("description = 28 Tooth Sprocket (custom)") Case Else description = "out of range" printValue("description = Invalid value. " + stringValue(nTeeth) _ + " teeth - " + description) End Select
An Exit statement can also be used to exit from a Try...Catch statement. Exit Try is allowed in any clause (Try, Catch, Finally). If a Finally block exists, it is executed regardless of whether an Exit Try statement appears in a Try or Catch clause.
Try If x < 4 Then Exit Try Else Error End If Catch Error printValue("Value exceeds limit.") Finally printValue("x = " + stringValue(x)) End Try
When an Exit statement is used within the body of a method, function, or rule, control returns to the invoking expression.
The return value must already have been set at the point the Exit is encountered; otherwise an error is generated.
‘Exit from a function. Exit Function ‘Exit from a method. Exit Method ‘Exit from a rule. Exit Rule