Vb.net 如何在抛出异常后继续运行代码?

Vb.net 如何在抛出异常后继续运行代码?,vb.net,exception,Vb.net,Exception,我想知道是否有办法让程序在抛出异常后继续运行。例如: 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 lo

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

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
试试看
第1行
第2行
第3行
第4行(这里抛出异常并跳转到catch)

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

试试看
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
第1行 特例 日志(例如tostring) 结束尝试 尝试 第2行 特例 日志(例如tostring) 结束尝试 尝试 第3行 特例 日志(例如tostring) 结束尝试 尝试 第4行(这里的异常是抛出并跳转到捕获) 特例 日志(例如tostring) 结束尝试 尝试
第5行“虽然错误恢复下一步的
是,但它与结构化异常处理的首选方法是互斥的

相反,我建议使用块的
Finally
子句来确保
第5行和第6行
得到执行,即使第4行(或任何前面的行)抛出


如果你正在做一些你知道如何从中恢复过来或者不重要的事情,你应该在try/catch中用一个特定的catch来包装这一行。 e、 g

试试看
第1行
第2行
第3行
尝试
第4行(这里的异常是抛出并跳转到捕获)
将iox捕获为“IOException”或抛出的任何类型
“记下来
结束尝试
第5行“如果我没有弄错,“处理异常的最佳实践”中说,如果您可以检查可能发生的错误,那么请检查该情况。如果您可以检查dbnull,那么请执行此操作。

使用“Continue for”

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

    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

下面是一个代码示例:

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

这是一个相当古老的职位,但为了其他人。
就我个人而言,我会使用“错误恢复下一步”在这种情况下,这是一个必要的邪恶

在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()来查看错误行编写器(不要写这个数字/标签)

aww…你比我强:),如果您不想创建所有的try-catch块,那么总是会出现可怕的goto语句。但我不这么认为!在这种情况下,从dB读取数据后,dbnullconversion将异常转换为int。。。但这只是许多其他数据中的一个,这也是我想继续阅读的原因。。谢谢欢迎评论!在任何情况下,专业开发人员都不会使用“出错后继续下一步”。一旦使用此选项,所有错误都将被忽略。。。。。粗略地翻译它的意思是,“我有一个错误,我不在乎”……我相信这是多余的。_TryCatch在for循环中,因此在处理异常之后,您将点击End Try,然后无论如何都会点击Next。唯一有用的是,如果不想继续ForLoop,那么可以使用EXIT FOR跳出循环。
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
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