开发者

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


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


VB.net does not support this type of construct. Once the exception unwinds the stack, it can't be unwound back again. Some languages do permit you to resume the exception, but they require more sophisticated stack management – essentially coroutines.


If I am not mistaken the "Best Practices for Handling Exceptions" says if you can check for an error that will likely occur then check for that condition. If you can check for dbnull then do so.


Here is an example in 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


In VB.NET you can use VISUAL BASIC 6.0 Code:

PRIVATE SUB PROCESO
ON ERROR GOTO VERERROR:
10: line1
20: line2 
30: line3
EXIT SUB
VERERROR:
MSGBOX("ERROR " & ERR.NUM & "." & ERR.DESCRIPTION)
RESUME NEXT 
'RESUME FOR RETRY
END SUB 

And you can use ERL() for view err line writer before of code '10:' (o no write this numbers/labels)


quite an old post but for the sake of others . personally i would use "on error resume next" in this case it is a necessary evil

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜