当 "On error resume next" 处于活动状态时,不会引发错误(运行-时间错误 5)

Error (Run-time error 5) will not raise when "On error resume next" is active

我在下面提供了我正在使用的实际代码。 我要处理的确切条件是 strCurrentRev 参数作为零长度字符串。 (例如 strCurrentRev=""

如果我注释掉错误处理语句,尝试对零长度字符串执行 ASC 方法会抛出 Run-Time Error 5 for "invalid procedure call or argument"。
如果我然后检查 err.Number 它是 = 5。

如果我尝试 运行 与 on error resume next 活动的完全相同的语句,它不会引发任何错误,例如执行后 err.number 总是 = 0。

如果 on error resume next 处于活动状态,并且您尝试从立即 window 执行 ASC 方法(例如键入 asc(strcurrentrev) 并点击 Enter),它将抛出运行-时间错误,将err.number属性设置为5。

我以前从未经历过这种情况。为什么 on error resume next 处于活动状态会导致在正常执行时不会出现错误???

Function NextRevLetter(strCurrentRev As String) As String
'This function returns the next revision letter given an existing revision letter.

'Declare variables
    Dim lngX As Long
    Dim strX As String
    Dim strY As String

'First, check if we are dealing with rev A-Z or AA-ZZ
        If Len(strCurrentRev) <= 1 Then
        'Check that we can work with revision letter ***THIS IS WHERE I AM HAVING A PROBLEM!***
            On Error Resume Next
            Err.Clear
            'Procedure call to flag errors with ASC method without changing any values
                lngX=Asc(strCurrentRev) 
                lngX=0
            On Error GoTo 0
                If Err.Number > 0 Then
                    Err.Clear
                        If Len(strCurrentRev) < 1 Then
                        'No revision specified, assign first revision"
                            strCurrentRev = "-"
                        Else
                            MsgBox "The revision letter specified is not compliant.  The next revision letter cannot be determined.", vbOKOnly, "Error: Revision does not follow rules"
                        'Return the existing revision (no change) and exit function
                            NextRevLetter = strCurrentRev
                            Exit Function
                        End If
                End If

            'Code continues - not important for this question...
Exit Function

我刚刚弄明白了。 On Error GoTo 0 语句将 Err.Number 属性 重置为 0。

抱歉浪费大家的时间!!!

您没有使用正确的工具来完成这项工作。运行时错误应该处理,而不是隐藏起来(因为那是On Error Resume Next所做的——执行愉快地继续,就好像什么都没发生一样) .

您首先需要尝试避免引发该错误。是什么原因造成的?

lngX=Asc(strCurrentRev) 

你已经知道发生了什么:

The exact condition I'm trying to handle is the strCurrentRev argument as a zero-length string.

那么,正确的处理方法是在 将 strCurrentRev 传递给 Asc 函数之前验证 strCurrentRev 的长度,[= =24=]你知道如果你给它一个空字符串会引发运行时错误#5!

If strCurrentRev <> vbNullString Then
    'calling Asc(strCurrentRev) here will not fail!
End If

我被要求详细说明处理错误的更好方法,这是最简单的地方。我认为没关系,因为在某种程度上它也确实回答了最初的问题。但是,让我先说这里正确的做法是完全避免错误,但为了完整起见,有一种方法可以 cleanly 使用错误处理程序来完成此操作。

想法是检查错误号,通过固定值来处理它,然后然后恢复下一行代码。

Function NextRevLetter(strCurrentRev As String) As String
'This function returns the next revision letter given an existing revision letter.

On Error GoTo ErrHandler

'Declare variables
    Dim lngX As Long
    Dim strX As String
    Dim strY As String

'First, check if we are dealing with rev A-Z or AA-ZZ
    If Len(strCurrentRev) <= 1 Then

        'Procedure call to flag errors with ASC method without changing any values
        lngX = Asc(strCurrentRev)
        lngX = 0

        'Code continues - not important for this question...

    End If

Exit Function

ErrHandler:
    If Err.Number = 5 Then
        lngX = 0
        If Len(strCurrentRev) < 1 Then
        'No revision specified, assign first revision"
            strCurrentRev = "-"
            Resume Next
        Else
            MsgBox "The revision letter specified is not compliant.  The next revision letter cannot be determined.", vbOKOnly, "Error: Revision does not follow rules"
            'Return the existing revision (no change) and exit function
            NextRevLetter = strCurrentRev
            Exit Function
        End If

    Else If Err.Number = someOtherExpectedError        
        'handle it appropriately

    Else
        ' !!! This is important.
        ' If we come across an error we don't know how to handle, we re-raise it.
        Err.Raise Err.Number, Err.Source, Err.Description
    End If

End Function

请注意,您的程序流不会被所有这些错误处理中断,我们只处理我们预期的错误。因此,如果出现错误,只有知道如何,我们才能恢复。否则,执行停止。

不过,我还是更愿意检查值是否为 = vbNullString