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

时间:2010-07-29 20:24:16

标签: 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
   log(ex.tostring)
End Try

9 个答案:

答案 0 :(得分:11)

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

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

答案 1 :(得分:6)

使用'继续'

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

    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

答案 2 :(得分:3)

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

相反,我建议使用Try..Catch..Finally块的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

答案 3 :(得分: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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

如果我没有弄错“处理异常的最佳做法”,如果您可以检查可能发生的错误,请检查该情况。如果你可以检查dbnull,那么就这样做。

答案 6 :(得分:0)

以下是代码中的示例:

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 :(得分:0)

在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()来查看err行编写器(不能写入此数字/标签)

答案 8 :(得分:-2)

相当古老的帖子,但为了别人的缘故。 我个人会使用“在错误恢复下”,在这种情况下,它是一个必要的邪恶