为什么 VBA 在没有错误的情况下会转到错误处理代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:33:26  来源:igfitidea点击:

Why VBA goes to error handling code when there is no error?

vbaerror-handling

提问by Martin08

I have writen some code in VBA (Excel) with error handling labels. It worked fine until I recently notice the error handling code gets executed everytime, not just when an error occurs. Does anybody know why this happens? Thanks.

我在 VBA (Excel) 中编写了一些带有错误处理标签的代码。它工作正常,直到我最近注意到错误处理代码每次都会执行,而不仅仅是在发生错误时。有人知道为什么会这样吗?谢谢。

Here's a trivial test case where bothmsgboxes would pop up.

这是一个简单的测试用例,其中两个msgboxes 都会弹出。

Sub example()
    On Error GoTo err_handle
    MsgBox "OK!"
err_handle:
MsgBox "not OK"
End Sub

回答by CAbbott

You want to add an Exit Sub to your routine:

您想将 Exit Sub 添加到您的例程中:

Sub example()
    On Error GoTo err_handle
    MsgBox "OK!"
    Exit Sub
    err_handle:
    MsgBox "not OK"
End Sub

Look herefor a full explaination.

在这里查看完整的解释。

回答by Alan Moore

It's because you aren't returning out of the subroutine after the first message box the (OK) one. After that is shown the next line of code is executed which is the "not ok" message.

这是因为您没有在第一个消息框(确定)之后返回子例程。之后显示下一行代码,即“not ok”消息。

You could either exit the subroutine early before your error handler label (ExitSub) or goto the end of the subroutine on success (bypassing the "error" routine)

您可以在错误处理程序标签 (ExitSub) 之前尽早退出子例程,或者在成功时转到子例程的末尾(绕过“错误”例程)

回答by Alan Moore

need to add exit subelse the program continues to execute the label as it is the part of the code

需要添加exit subelse 程序继续执行标签,因为它是代码的一部分

回答by JohnFx

Just as a clarification to add some meat to the other answers.

就像澄清在其他答案中添加一些肉一样。

"err_handle:" is only error handling code because you are using it as such. It isn't intrinsically an error handler like a catch block in other languages.

"err_handle:" 只是错误处理代码,因为您正在使用它。它本质上不是像其他语言中的 catch 块那样的错误处理程序。

Technically "err_handle:" is just a label that facilitates a goto jump. In your case the goto just happens to be used with an error handler (on error goto)

从技术上讲,“err_handle:”只是一个便于跳转的标签。在您的情况下,goto 恰好与错误处理程序一起使用(在错误 goto 上)

回答by Oorang

The generally accepted pattern for error handling is to have an error handler and an exit procedure. A pretty standard code stump might look like this:

普遍接受的错误处理模式是有一个错误处理程序和一个退出过程。一个非常标准的代码树桩可能如下所示:

Public Function Example() As String
    Dim strRtnVal As String 'Return value variable.
    On Error GoTo Err_Hnd
    '***************************************************************************
    'Lock interface code here (hourglass, screenupdating etc.)
    '***************************************************************************

    '***************************************************************************
    'Your code here.
    '***************************************************************************
Exit_Proc:
    'Prevents "Error Loops" caused by errors within the exit procedure:
    On Error Resume Next
    '***************************************************************************
    'Restore Interface.
    '***************************************************************************
    Example = strRtnVal 'Set Return Value.
    Exit Function
Err_Hnd:
    'Display message, do some logging, whatever.
    Resume Exit_Proc '<- Run exit procedure.
End Function