异常

2010-07-29 38 views
6

后继续代码,我想知道是否有办法让一个异常被抛出后的程序继续进行。例如:异常

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 loging the error 
    line 6 

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

谢谢。

回答

10

如果你这样做,你知道如何从恢复或不是重要的东西,你应该包装在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 loggin the error 
    line 6 

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

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

0
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 loggin the error 
catch ex as exception 
    log(ex.tostring) 
end try 
try 
    line 6 
catch ex as exception 
end try 
+7

你觉得有必要洗澡打字,经过? :) – 2010-07-29 20:28:39

+0

噢......你打我吧:) ,如果你不喜欢创建所有的try-catch块总是有可怕的goto语句。但我不会建议! – 2010-07-29 20:30:17

+0

UFF很好,好像是去..感谢 – carlos 2010-07-29 20:36:40

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 
+0

在这种情况下,从dB读取dbnullconversion为int后,出现异常......但这只是许多其他数据的一个数据,这就是为什么我想继续阅读.. Thanks!为评论! – carlos 2010-07-29 20:35:51

0

如果我没有记错的“最佳实践处理异常的说:”如果你可以检查一个错误,很可能会发生,然后检查这一条件。如果你可以检查dbnull然后这样做。

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 
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 
-1

相当旧的文章,但为他人着想。 我个人使用“的错误恢复下一步”,在这种情况下,它是一个必要的邪恶

+1

没有专业的开发者对错误继续下一步“在任何情况下将永远使用。一旦你使用这个所有的错误都被简单地忽略.....粗略地翻译它的意思是'我有一个错误,我不在乎'...... – Monty 2016-03-15 21:08:58