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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3366635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:58:25  来源:igfitidea点击:

How to continue running code after an exception is thrown?

vb.netexception

提问by carlos

I would like to know if there is a way to let the program continue after an exception is thrown. For example:

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

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

回答by Nikki9696

If you're doing something that you know how to recover from or that isn't vital, you're supposed to wrap just that line in the try/catch with a specific catch. e.g.

如果您正在做一些您知道如何从中恢复或不重要的事情,您应该在 try/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

回答by Eddie

Use 'Continue For'

使用“继续”

Not good practice everywhere, but useful in some circumstances, e.g. find a file while handling denied access to certain directories:

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

    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

回答by StuartLC

Although On Error Resume Nextis still available in VB.NET, it is mutually exclusive to the preferred method of structured exception handling.

虽然On Error Resume Next仍然在VB.NET可用,它是互斥的结构化异常处理的优选方法。

Instead, I would recommend the use of the Finallyclause of a Try..Catch..Finallyblock to ensure Line 5 and Line 6get executed even if Line 4 (or any preceding Line) throws.

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

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

回答by S.Lott

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

回答by Thom Smith

VB.net does not support this type of construct. Once the exception unwinds the stack, it can't be unwound back again. Some languages do permit you to resume the exception, but they require more sophisticated stack management – essentially coroutines.

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

回答by Alex

Here is an example in code:

这是代码中的示例:

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

回答by dbasnett

If I am not mistaken the "Best Practices for Handling Exceptions" says if you can check for an error that will likely occur then check for that condition. If you can check for dbnull then do so.

如果我没有记错的话,“处理异常的最佳实践”说您是否可以检查可能发生的错误,然后检查该条件。如果您可以检查 dbnull,请执行此操作。

回答by R.Alonso

In VB.NET you can use VISUAL BASIC 6.0 Code:

在 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 

And you can use ERL() for view err line writer before of code '10:' (o no write this numbers/labels)

您可以使用 ERL() 在代码 '10:' 之前查看错误行编写器(不要写这个数字/标签)

回答by davis

quite an old post but for the sake of others . personally i would use "on error resume next" in this case it is a necessary evil

很旧的帖子,但为了别人。在这种情况下,我个人会使用“下一个错误恢复”,这是一种必要的邪恶