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

时间:2012-01-27 15:25:29

标签: vb6 error-handling

我需要对一些旧的VB6代码进行故障排除,我对使用“On Error”感到困惑。在下面的示例中,如果我围绕要使用On Error GoTo和ErrHandler1进行测试的特定代码行,那么该行是经过测试的唯一行。或者,如果它在同一个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

感谢。

3 个答案:

答案 0 :(得分:8)

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

正确的方法就是那样

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)

答案 1 :(得分:5)

只有在MkDir不会引发错误时才会处理除零。

那说它将循环到ErrHandler1标签,并且将再次从除数生成另一个错误,这将不会被处理,因为您无法在另一个错误处理程序中嵌套错误处理。

因此代码没有意义,错误处理程序应该在代码中向下移动(在exit子下面),以确保它只被调用一次:

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)

答案 2 :(得分:1)

GoTo 错误处理程序,程序的顶部是良好的编程风格,应该是大多数过程的最小错误处理量。但是,它比在可能导致错误的代码行之后检查错误更不灵活。在一个相当简单的过程中添加错误处理时,我使用 On Error GoTo ...语句和例程底部的catch-all错误处理程序。

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 语句,因为它使代码难以阅读和遵循。在我执行几个步骤的过程中,我想返回一个更准确地描述代码出错位置的错误,或者在我可能有机会从错误中恢复并继续的情况下,我禁用了catch-all错误处理的类型,并在关键步骤后检查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