Continue code after exception

I would like to know if there is a way for the program to continue after the 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 loging the error line 6 Catch ex as Exception log(ex.tostring) End Try 

Thanks.

+6
exception-handling
source share
8 answers

If you are doing something that you know how to recover or it is not vital, you should only transfer this line to try / catch with a certain catch. eg.

 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 
+10
source share

Use 'Continue For'

Bad practice is everywhere, but useful in some cases, for example. find a file while processing restricted 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 
+6
source share

Although On Error Resume Next is still available in VB.NET , it is mutually exclusive for the preferred method for handling structured exceptions.

Instead, I would recommend using the Finally Try..Catch..Finally block to ensure that Line 5 and Line 6 executed even if row 4 (or any previous row) is selected.

 Try line 1 line 2 line 3 line 4 Catch ex as Exception log(ex.tostring) Finally line 5 line 6 End Try 
+3
source share

VB.net does not support this type of design. Once an exception expands the stack, it cannot be rolled back. Some languages ​​allow you to resume an exception, but this requires more sophisticated stack management - essentially a coroutine.

0
source share
 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 
0
source share

If I'm not mistaken, Exception Guidelines says that if you can check for the error that is most likely to happen, check this condition. If you can check dbnull do it.

0
source share

Here is a sample code:

 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 
0
source share

pretty old post, but for the sake of others. I will personally use "on error resume next", in this case it is a necessary evil

-one
source share

All Articles