carlos carlos - 1 month ago 8
Vb.net Question

Continue Code after Exception

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

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


Thanks.

Answer

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

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