尽管使用GOTO子句不是一种不错的方法,但出于好奇,我想问一个问题,为什么使用GOTO子句的倒数第三条不能在下面的过程中工作。
子过程计算一个正数的平方根。如果给出负数,则错误控制会话将生效,询问用户是否将再次重新实现该子对象。我首先输入-1,它会提示询问“您是否想再试一次?”;稍后输入-2,发生意外错误。 click for the picture of the unexpected error。如果我将GOTO替换为“ resume”或调用子程序本身,则不会发生意外错误。
一种解释是“因为未清除原始错误条件”。 (约翰·沃肯巴赫)。有人可以对此提供进一步的说明吗?
Sub enterSqrt3()
Dim pos_num As Variant, ans As Double, decision As Variant
TryAgain:
On Error GoTo errHandle
pos_num = InputBox("Please input a positive number. ")
If pos_num = "" Then
Exit Sub
Else
ans = Sqr(pos_num)
MsgBox "The answer is " & ans
Exit Sub
End If
errHandle:
decision = MsgBox("Would you like to try again? ", vbYesNo)
If decision = vbYes Then
' Resume TryAgain ' it works
' Call enterSqrt3 ' it works
GoTo TryAgain ' it does not work after input the second
' negative input.
End If
End Sub