对 VB6 中的错误处理和 On Error GoTo 的使用感到困惑
我需要对一些旧的 VB6 代码进行故障排除,但我对“On Error”的使用感到困惑。在下面的示例中,如果我围绕要使用 On Error GoTo 和 ErrHandler1 进行测试的特定代码行,则这是唯一经过测试的行。或者如果它在同一个子中,除以零是否会被包括在内?
On Error GoTo ErrHandler1
If Not Exists(BaseDirectory + "\ARCHIVE") Then _
MkDir BaseDirectory + "\ARCHIVE"
ErrHandler1:
Call MsgBox(Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
intValue1 = 12
intValue2 = 0
intValue3 = intValue1 / intValue
谢谢。
I'm needing to troubleshoot some old VB6 code and I'm confused about the use of "On Error". In the sample below, if I surround the specific line of code I want to test with the On Error GoTo and the ErrHandler1, is that the ONLY line that's tested. Or will the divide by Zero be included if it's in the same Sub?
On Error GoTo ErrHandler1
If Not Exists(BaseDirectory + "\ARCHIVE") Then _
MkDir BaseDirectory + "\ARCHIVE"
ErrHandler1:
Call MsgBox(Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
intValue1 = 12
intValue2 = 0
intValue3 = intValue1 / intValue
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除以零将被包含并处理,它可能会在您的示例中创建一个循环......
正确的方法是这样的
The divide by zero will be included and handled, and it will probably create a loop in your sample...
The correct approach is like that
仅当 MkDir 不会导致抛出错误时才会处理除以零。
也就是说,它将循环到 ErrHandler1 标签,并且再次除以零时将生成另一个错误,该错误将不会被处理,因为您无法将错误处理嵌套在另一个错误处理程序中。
因此,该代码就其本身而言没有意义,错误处理程序应进一步移至代码下方(在退出子下方),以确保它只被调用一次:
如果您想分别处理这两个操作,您可以这样做:
The division by zero is only handled if the MkDir does not cause an error to be thrown.
That said it will loop to the
ErrHandler1
label and another error will be generated from the divide by zero again which will not be handled because you can't nest error handling within another error handler.The code therefore does not make sense as it stands, the error handler should be moved further down the code (below an exit sub) to make sure it only gets called once:
If you want to handle both operations separately you can do this:
位于过程顶部的 GoTo 错误处理程序是良好的编程风格,对于大多数过程来说应该是最少的错误处理量。然而,它不如在可能导致错误的代码行之后检查错误灵活。在相当简单的过程中添加错误处理时,我使用 On Error GoTo ... 语句和例程底部的捕获所有错误处理程序。
我不喜欢多个 GoTo 语句,因为它使代码难以阅读和遵循。在一个过程中,我正在执行多个步骤,并且我想返回一个错误来更准确地描述代码出错的位置,或者在我可能有机会从错误中恢复并继续的情况下,我禁用了包罗万象的功能错误处理类型并在关键步骤后检查 Err.Number 属性。如果我修改 Matt 的错误处理,我会以这种方式编写过程。
A GoTo error handler it the top of a procedure is good programming style and should be the minimum amount of error handling for most procedures. However it is less flexible than checking for errors after lines of code that could cause an error. When adding error handling in a fairly simple procedure I use the On Error GoTo ... statement and a catch-all error handler at the bottom of the routine.
I am not a fan of multiple GoTo statements because it makes code difficult to read and follow. In a procedure where I am performing several steps and I want to return an error that more accurately describes where the code went wrong, or in a situation where I might have a chance to recover from the error and continue, I disable the catch-all type of error handling and check the Err.Number property after critical steps. If I modify Matt's error handling I would code the procedure this way.