对 VB6 中的错误处理和 On Error GoTo 的使用感到困惑

发布于 2024-12-29 11:36:54 字数 457 浏览 0 评论 0原文

我需要对一些旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2025-01-05 11:36:54

除以零将被包含并处理,它可能会在您的示例中创建一个循环......

正确的方法是这样的

On Error GoTo ErrHandler1
If Not Exists(BaseDirectory + "\ARCHIVE") Then _
   MkDir BaseDirectory + "\ARCHIVE"

On Error Goto 0 'this will un-hook you error handler

intValue1 = 12
intValue2 = 0
intValue3 = intValue1 / intValue  'this will be an un-managed error

Exit Sub 'this make sure that msgbox is shown only when the error happens

ErrHandler1:
   Call MsgBox(Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)

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

On Error GoTo ErrHandler1
If Not Exists(BaseDirectory + "\ARCHIVE") Then _
   MkDir BaseDirectory + "\ARCHIVE"

On Error Goto 0 'this will un-hook you error handler

intValue1 = 12
intValue2 = 0
intValue3 = intValue1 / intValue  'this will be an un-managed error

Exit Sub 'this make sure that msgbox is shown only when the error happens

ErrHandler1:
   Call MsgBox(Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
凯凯我们等你回来 2025-01-05 11:36:54

仅当 MkDir 不会导致抛出错误时才会处理除以零。

也就是说,它将循环到 ErrHandler1 标签,并且再次除以零时将生成另一个错误,该错误将不会被处理,因为您无法将错误处理嵌套在另一个错误处理程序中。

因此,该代码就其本身而言没有意义,错误处理程序应进一步移至代码下方(在退出子下方),以确保它只被调用一次:

On Error Goto ErrHandler1

    'some code

    exit sub
ErrHandler1:
    msgbox "There was an error"

如果您想分别处理这两个操作,您可以这样做:

On Error GoTo ErrHandler1
If Not Exists(BaseDirectory + "\ARCHIVE") Then _
   MkDir BaseDirectory + "\ARCHIVE"

DoCalc:
    On Error GoTo Errhandler2
    intvalue1 = 12
    intvalue2 = 0
    intvalue3 = intvalue1 / intvalue

    Exit Sub

ErrHandler1:
   Call MsgBox("Error making directory - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
   Resume DoCalc:

Errhandler2:
    Call MsgBox("Error doing arithmetic - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)

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:

On Error Goto ErrHandler1

    'some code

    exit sub
ErrHandler1:
    msgbox "There was an error"

If you want to handle both operations separately you can do this:

On Error GoTo ErrHandler1
If Not Exists(BaseDirectory + "\ARCHIVE") Then _
   MkDir BaseDirectory + "\ARCHIVE"

DoCalc:
    On Error GoTo Errhandler2
    intvalue1 = 12
    intvalue2 = 0
    intvalue3 = intvalue1 / intvalue

    Exit Sub

ErrHandler1:
   Call MsgBox("Error making directory - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
   Resume DoCalc:

Errhandler2:
    Call MsgBox("Error doing arithmetic - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
陌若浮生 2025-01-05 11:36:54

位于过程顶部的 GoTo 错误处理程序是良好的编程风格,对于大多数过程来说应该是最少的错误处理量。然而,它不如在可能导致错误的代码行之后检查错误灵活。在相当简单的过程中添加错误处理时,我使用 On Error GoTo ... 语句和例程底部的捕获所有错误处理程序。

On Error GoTo procErrorHandler

    If Not Exists(BaseDirectory + "\ARCHIVE") Then
        MkDir BaseDirectory + "\ARCHIVE"
    End If

    intvalue1 = 12
    intvalue2 = 0
    intvalue3 = intvalue1 / intvalue

ProcExit:
    Exit Sub

procErrorHandler:
    Call MsgBox("There was an error in the procedure. Error " & CStr(Err.Number) & ", " & Err.Description, vbExclamation, App.Title)
    Resume ProcExit  ' A chance to do any cleanup needed

我不喜欢多个 GoTo 语句,因为它使代码难以阅读和遵循。在一个过程中,我正在执行多个步骤,并且我想返回一个错误来更准确地描述代码出错的位置,或者在我可能有机会从错误中恢复并继续的情况下,我禁用了包罗万象的功能错误处理类型并在关键步骤后检查 Err.Number 属性。如果我修改 Matt 的错误处理,我会以这种方式编写过程。

On Error Resume Next

    If Not Exists(BaseDirectory + "\ARCHIVE") Then
        MkDir BaseDirectory + "\ARCHIVE"
    End If
    ' check for errors making the directory
    If Err.Number <> 0 Then
        Call MsgBox("Error making directory - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
    End If

    intvalue1 = 12
    intvalue2 = 0
    intvalue3 = intvalue1 / intvalue
    ' check for errors getting intvalue3
    If Err.Number <> 0 Then
        Call MsgBox("Error doing arithmetic - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
    End If

Exit Sub

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.

On Error GoTo procErrorHandler

    If Not Exists(BaseDirectory + "\ARCHIVE") Then
        MkDir BaseDirectory + "\ARCHIVE"
    End If

    intvalue1 = 12
    intvalue2 = 0
    intvalue3 = intvalue1 / intvalue

ProcExit:
    Exit Sub

procErrorHandler:
    Call MsgBox("There was an error in the procedure. Error " & CStr(Err.Number) & ", " & Err.Description, vbExclamation, App.Title)
    Resume ProcExit  ' A chance to do any cleanup needed

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.

On Error Resume Next

    If Not Exists(BaseDirectory + "\ARCHIVE") Then
        MkDir BaseDirectory + "\ARCHIVE"
    End If
    ' check for errors making the directory
    If Err.Number <> 0 Then
        Call MsgBox("Error making directory - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
    End If

    intvalue1 = 12
    intvalue2 = 0
    intvalue3 = intvalue1 / intvalue
    ' check for errors getting intvalue3
    If Err.Number <> 0 Then
        Call MsgBox("Error doing arithmetic - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
    End If

Exit Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文