我想知道是否有办法让程序在抛出异常后继续.例如:
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
Run Code Online (Sandbox Code Playgroud)
谢谢.
Nik*_*696 11
如果你正在做一些你知道如何恢复或者不重要的事情,那么你应该在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
Run Code Online (Sandbox Code Playgroud)
小智 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41927 次 |
| 最近记录: |