【问题标题】:How to use multiple error handlers in VBA如何在 VBA 中使用多个错误处理程序
【发布时间】:2020-03-28 10:47:35
【问题描述】:

我尝试在 VBA 代码中使用多个错误处理

Sub ErrorTestMultiple()

Test1:
    On Error GoTo ErrHandler1
        y = 6 / 0
        GoTo Test2

ErrHandler1:
        Cells(4, "H") = "Test 1 failed"
        Cells(4, "I") = Err.Description


Test2:

    On Error GoTo ErrHandler2
        y = 6 / 0 ' Process stop here - Why is the 2nd Error handler not working?
        GoTo Test3

ErrHandler2:
        Cells(5, "H") = "Test 2 failed"
        Cells(5, "I") = Err.Description

Test3:

End Sub

我尝试在here 中添加以下示例的简历,但没有成功

我怎样才能让它工作?

【问题讨论】:

    标签: vba


    【解决方案1】:

    您需要事先使用On Errro Goto -1 重置错误处理程序,即

    Sub ErrorTestMultiple()
    
    Test1:
        On Error GoTo ErrHandler1
            y = 6 / 0
            GoTo Test2
    
    ErrHandler1:
            Cells(4, "H") = "Test 1 failed"
            Cells(4, "I") = Err.Description
    
    
    Test2:
    
        On Error GoTo -1
        On Error GoTo ErrHandler2
    
            y = 6 / 0 ' Process stop here - Why is the 2nd Error handler not working?
            GoTo Test3
    
    ErrHandler2:
            Cells(5, "H") = "Test 2 failed"
            Cells(5, "I") = Err.Description
    
    Test3:
    
    End Sub
    

    但是这是不好的做法。延伸阅读here

    PS你可以像这样重新构造上面的代码

    Sub ErrorTestMultiple()
    
        Dim y As Double
    
        On Error GoTo ErrorHandler
    
        y = 6 / 0
        y = 6 / 0
    
        Exit Sub
    
    ErrorHandler:
    
        Dim j As Long
        j = j + 1
    
        Cells(3 + j, "H") = "Test " & j & " failed"
        Cells(3 + j, "I") = Err.Description
    
        Resume Next
    
    End Sub
    

    【讨论】:

      【解决方案2】:
      Sub ErrorTestMultiple()
      
      Test1:
          On Error GoTo ErrHandler1
              y = 6 / 0
          on error goto 0
              GoTo Test2
      
      ErrHandler1:
              Cells(4, "H") = "Test 1 failed"
              Cells(4, "I") = Err.Description
      
               Resume next
      
      Test2:
      
          On Error GoTo ErrHandler2
              y = 6 / 0
          on error goto 0
              GoTo Test3
      
      ErrHandler2:
              Cells(5, "H") = "Test 2 failed"
              Cells(5, "I") = Err.Description
      
               Resume next
      Test3:
      
      End Sub
      

      【讨论】:

        【解决方案3】:

        按照 Storax 的建议。

        每个子例程应用一个错误处理程序。因此,错误处理将仅适用于错误调用的第一个实例。

        所以为了处理多个错误调用。首先,尝试将您的子程序分解为更小的程序。

        因此,如果您知道会发生什么或程序的预期行为,则错误处理的主要规则无需在“运行时”期间处理它。

        在大多数情况下,适当的代码结构可能会奏效。

        【讨论】:

          【解决方案4】:

          您可以在一个过程中拥有多个错误处理程序。诀窍是避免 Goto Label 的常见示例。相反,您应该使用 On Error Resume Next 和 On Error Goto O 的组合来模拟 try catch 块。

          Sub ErrorTestMultiple()
          
              ' Test 1
              On Error Resume Next
              Y = 6 / 0
              If Err.Number <> 0 Then
                  On Error GoTo 0
                  Cells(4, "H") = "Test 1 failed"
                  Cells(4, "I") = Err.Description
                  Exit Sub
          
              End If
              On Error GoTo 0
              ' Test 2
              On Error Resume Next
              Y = 6 / 0 
              If Err.Number <> 0 Then
                  On Error GoTo 0
                  Cells(5, "H") = "Test 2 failed"
                  Cells(5, "I") = Err.Description
                  Exit Sub
              End If
              On Error GoTo 0
          
          End Sub
          

          这里有一些关于 Goto ErrorHandler 替代策略的好读物

          https://rubberduckvba.wordpress.com/2019/05/

          【讨论】:

            猜你喜欢
            • 2016-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-23
            • 2021-10-04
            相关资源
            最近更新 更多