Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue running code after an exception is thrown?

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
like image 491
carlos Avatar asked Jul 29 '10 20:07

carlos


People also ask

How do you continue execution after throwing exception?

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.

Does code continue after throwing an exception?

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.

How do you continue if exception occurs?

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.

Is it possible to resume Java execution after exception occurs?

No. You cannot just jump back into the middle of a method.


4 Answers

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
like image 65
Nikki9696 Avatar answered Sep 28 '22 08:09

Nikki9696


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
like image 31
Eddie Avatar answered Sep 28 '22 08:09

Eddie


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
like image 29
StuartLC Avatar answered Sep 28 '22 10:09

StuartLC


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
like image 35
S.Lott Avatar answered Sep 28 '22 10:09

S.Lott