代码之家  ›  专栏  ›  技术社区  ›  bjan

如何更换末端

vb6
  •  1
  • bjan  · 技术社区  · 8 年前

    End 终止。

    有什么办法可以让我改变这种行为吗 ?

    终点 正在使用而不是显示错误,然后通过使用一些关键字/技术,我返回到第一个调用方(而不是在使用时发生的最后一个调用方) Exit Sub/Function )

    Private Sub Button_Click()
      Call DoSomething()
    End Sub
    
    Public Sub DoSomething()
      Call DoThing_1()
      Call DoThing_2()
    End Sub
    
    Public Sub DoThing_1()
      Call DoThing_A()
      ...
      Call DoThing_B()
      ...
      MsgBox "Error Here"
      'End  
      Exit Sub 'As i have asked(if possible), instead of returning to DoSomething(), the call should return to Button_Click
      ...
      Call DoThing_C()
      ...
    End Sub
    
    2 回复  |  直到 8 年前
        1
  •  4
  •   Tomalak    8 年前

    VB中错误管理和恢复的标准方法是使用 On Error

    引发的错误将在调用堆栈中向上移动,直到它碰到通过定义了错误处理程序的函数为止 出错时 ,或直到调用堆栈为空,此时程序将终止并出现错误。

    所以如果你更换 End 具有 Err.Raise MSDN 在VBA上,由于MS非常小心地删除了实际的VB语言引用,因此可以执行以下操作:

    Option Explicit
    
    ' you don't need to, but it's good style to have meaningful error numbers
    ' (vbObjectError is the error number after which user-defined errors may exist)
    Const MY_ERROR_TYPE_A As Integer = vbObjectError + 1
    Const MY_ERROR_TYPE_B As Integer = vbObjectError + 2
    
    Public Sub Button_Click()
      On Error GoTo Err_Button_Click
    
      Call DoSomething
    
      Exit Sub
    
    Err_Button_Click:
      Select Case Err.Number
        Case MY_ERROR_TYPE_A:
          MsgBox "Error during Button_Click: " & vbNewLine & _
            Err.Description & vbNewLine & _
            "in " & Err.Source, vbExclamation, "Oops"
          Err.Clear
          Resume Next
        Case MY_ERROR_TYPE_B:
          MsgBox Err.Description, vbCritical, "This is really bad."
          End
      End Select
    End Sub
    
    Public Sub DoSomething()
      Call DoThing_1
      Call DoThing_2
    End Sub
    
    Public Sub DoThing_1()
      Dim frob
    
      Call DoThing_A
      '...
      Call DoThing_B
      '...
      If frob = 0 Then
        Err.Raise MY_ERROR_TYPE_A, "DoThing_1", "Could not frob the buzz"
      End If
      '...
      Call DoThing_C
      '...
    End Sub
    

    在这里 Button_Click 定义错误处理程序。通常,这是一个代码块,只能由某个标签访问,即存在一个 Exit Sub / Exit Function 在它前面,你需要 GoTo 去那里。在该块中,您可以向用户显示有意义的消息,并通过 Resume Next

    它不必像上面的示例那样详细。你可以实现它的一个最小版本-没有错误常量,没有消息,只是 呃,升起

        2
  •  0
  •   Jason Foster    8 年前

    我在这里遵循的模式是将sub改为函数,并让每个sub返回一个true/false值,指示程序是否应该继续。另外,存储错误的全局变量也可以工作,但我更喜欢带有错误消息的out参数。

    所以,如果我们使用我的模式,你的代码会变成:

    Private Sub Button_Click()
      Call DoSomething()
    End Sub
    
    Public Sub DoSomething()
        Dim strErrDescription as String
        If DoThing_1(strErrDescription) Then
            If DoThing_2(strErrDescription) Then
                //Could put a DoThing_3() 'here if desired
            End If
        End if
        'Do something with strErrDescription now
    End Sub
    
    'Returns true if we should continue, false if an error happened and we should stop
    Public Function DoThing_1(ByRef strErrorDescriptionOut) as Boolean 
      DoThing_1 = True 'Callers continue unless we encounter an error
      Call DoThing_A() 'Note: If you want to follow the same pattern, you can add an if here as well
      ...
      Call DoThing_B() 'Note: If you want to follow the same pattern, you can add an if here as well
      ...
      strErrorDescriptionOut = "Error Here"
      'End  
      DoThing_1 = false
      Exit Function 
      ...
      Call DoThing_C()
      ...
    End Function