【问题标题】:How to continue running code after an exception is thrown?抛出异常后如何继续运行代码?
【发布时间】:2011-03-22 22:50:46
【问题描述】:

我想知道是否有办法让程序在抛出异常后继续运行。例如:

Try
  line 1
  line 2
  line 3
  line 4 ' (here the exception is thrown and jumps to the catch)
  line 5 ' <-- I would like the program to continue its execution, logging the error
  line 6  

Catch ex as Exception
   log(ex.tostring)
End Try

【问题讨论】:

    标签: vb.net exception


    【解决方案1】:
    try 
      line 1
    catch ex as exception
       log(ex.tostring)
    end try
    try
      line 2
    catch ex as exception
       log(ex.tostring)
    end try
    try
      line 3
    catch ex as exception
       log(ex.tostring)
    end try
    try
      line 4 ' (here the exception is throw and jumps to the catch)
    catch ex as exception
       log(ex.tostring)
    end try
    try
      line 5 ' <-- I would like the program to continue its execution after logging the error
    catch ex as exception
       log(ex.tostring)
    end try
    try
      line 6  
    catch ex as exception
    end try
    

    【讨论】:

    • aww...你打败了我 :) 如果你不想创建所有的 try-catch 块,那么总会有可怕的 goto 语句。但我不建议这样做!
    【解决方案2】:

    如果您正在做一些您知道如何从中恢复或不重要的事情,那么您应该在 try/catch 中只用特定的 catch 包装该行。 例如

    Try 
      line 1
      line 2
      line 3
      Try
         line 4 ' (here the exception is throw and jumps to the catch)
      Catch iox as IOException ' or whatever type is being thrown
         'log it
      End Try
      line 5  ' <-- I would like the program to continue its execution after logging the error
      line 6  
    
    Catch ex as Exception
       log(ex.tostring)
    End Try
    

    【讨论】:

      【解决方案3】:

      在 VB.NET 中,您可以使用 VISUAL BASIC 6.0 代码:

      PRIVATE SUB PROCESO
      ON ERROR GOTO VERERROR:
      10: line1
      20: line2 
      30: line3
      EXIT SUB
      VERERROR:
      MSGBOX("ERROR " & ERR.NUM & "." & ERR.DESCRIPTION)
      RESUME NEXT 
      'RESUME FOR RETRY
      END SUB 
      

      您可以在代码“10:”之前使用 ERL() 查看错误行编写器(o 不写此数字/标签)

      【讨论】:

        【解决方案4】:

        虽然On Error Resume Nextstill available in VB.NET,但它与结构化异常处理的首选方法是互斥的。

        相反,我建议使用 Try..Catch..Finally 块的 Finally 子句,以确保即使第 4 行(或任何前面的行)抛出,Line 5 and Line 6 也能执行。

        Try
          line 1
          line 2
          line 3
          line 4
        Catch ex as Exception
           log(ex.tostring)
        Finally
          line 5
          line 6  
        End Try
        

        【讨论】:

        • 在这种情况下,异常是由从 dB 读取后的 dbnullconversion 到 int 造成的……但这只是许多其他数据的一个数据,这就是我想继续阅读的原因……谢谢! !为 cmets !
        【解决方案5】:

        相当老的帖子,但为了别人。 在这种情况下,我个人会使用“on error resume next”,这是一种必要的邪恶

        【讨论】:

        • 没有专业的开发人员会在任何情况下使用“on error resume next”。一旦你使用这个 ALL 错误就会被忽略.....粗略翻译它的意思是,'我有一个错误,我不在乎'......
        【解决方案6】:

        下面是代码示例:

        Sub yourSub()
          Dim cDelegate As CatchDelegate = Sub(ex As Exception)
                                               Your Catch Code
                                           End Sub
         line 1
         line 2
         line 3
         TCResumeNext(Sub() line 4, cDelegate)
         line 5
         line 6
        End Sub
        
        Delegate Sub CatchDelegate(e As Exception)
        
        Sub TCResumeNext(tryDelegate As [Delegate], catchDelgate As CatchDelegate)
           Try
             tryDelegate.DynamicInvoke()
           Catch ex As Exception
              catchDelgate.DynamicInvoke(ex)
           End Try
        End Sub
        

        【讨论】:

          【解决方案7】:

          使用“继续”

          在所有地方都不是很好的做法,但在某些情况下很有用,例如在处理对某些目录的拒绝访问时查找文件:

              Dim dir As New DirectoryInfo("C:\")
              Dim strSearch As String = ("boot.ini")
          
              For Each SubDir As DirectoryInfo In dir.GetDirectories
                  Try
                      For Each File As FileInfo In SubDir.GetFiles
                          Console.WriteLine("Sub Directory: {0}", SubDir.Name)
                          If File.Name = strSearch Then
                              Console.Write(File.FullName)
                          End If
                      Next
                  Catch ex As Exception
                      Console.WriteLine(ex.Message)
                      Continue For
                  End Try
              Next
          

          【讨论】:

          • 我认为这是多余的。 The_TryCatch inside for 循环,因此在处理异常后,您将点击 End Try,然后无论如何您都会点击 Next。唯一有用的是如果您不想继续 ForLoop ,那么您将使用 EXIT FOR 退出循环。
          【解决方案8】:

          如果我没记错的话,“处理异常的最佳实践”说是否可以检查可能发生的错误,然后检查该情况。如果您可以检查 dbnull,那么请这样做。

          【讨论】:

            【解决方案9】:

            VB.net 不支持这种类型的构造。一旦异常展开堆栈,它就不能再次展开。有些语言确实允许您恢复异常,但它们需要更复杂的堆栈管理——本质上是协程。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-05-13
              • 1970-01-01
              • 1970-01-01
              • 2016-03-18
              • 2012-03-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多