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
Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.
An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.
By default, you put an exception handler at the end of a subprogram to handle exceptions that are raised anywhere inside the subprogram. To continue execution from the spot where an exception occurred, enclose the code that might raise an exception inside another BEGIN-END block with its own exception handler.
No. You cannot just jump back into the middle of a method.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With