How to continue running code after an exception is thrown?
Although On Error Resume Next
is still available in VB.NET, it is mutually exclusive to the preferred method of structured exception handling.
Instead, I would recommend the use of the Finally
clause of a Try..Catch..Finally
block to ensure Line 5 and Line 6
get executed even if Line 4 (or any preceding Line) throws.
Try
line 1
line 2
line 3
line 4
Catch ex as Exception
log(ex.tostring)
Finally
line 5
line 6
End Try
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
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
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