異常

2010-07-29 60 views
6

後繼續代碼,我想知道是否有辦法讓一個異常被拋出後的程序繼續進行。例如:異常

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 

謝謝。

回答

10

如果你這樣做,你知道如何從恢復或不是重要的東西,你應該包裝在try/catch語句只是符合特定的漁獲物。 例如

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 
0

VB.net不支持這種類型的構造。一旦異常展開堆棧,它就不能再次展開。有些語言允許你恢復異常,但它們需要更復雜的堆棧管理 - 本質上是協程。

0
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 
+7

你覺得有必要洗澡打字,經過? :) – 2010-07-29 20:28:39

+0

噢......你打我吧:) ,如果你不喜歡創建所有的try-catch塊總是有可怕的goto語句。但我不會建議! – 2010-07-29 20:30:17

+0

UFF很好,好像是去..感謝 – carlos 2010-07-29 20:36:40

3

雖然On Error Resume Nextstill available in VB.NET,它是相互排斥的結構化異常處理的優選方法。

相反,我會建議使用Try..Catch..Finally塊的Finally條款,以確保Line 5 and Line 6得到執行,即使4號線(或前行)拋出。

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

在這種情況下,從dB讀取dbnullconversion爲int後,出現異常......但這只是許多其他數據的一個數據,這就是爲什麼我想繼續閱讀.. Thanks!爲評論! – carlos 2010-07-29 20:35:51

0

如果我沒有記錯的「最佳實踐處理異常的說:」如果你可以檢查一個錯誤,很可能會發生,然後檢查這一條件。如果你可以檢查dbnull然後這樣做。

6

使用「繼續」

不是好的做法隨處可見,但有用的在某些情況下,例如找到一個文件,而處理拒絕訪問某些目錄:

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 
0

這裏是代碼的例子:

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 
-1

相當舊的文章,但爲他人着想。 我個人使用「的錯誤恢復下一步」,在這種情況下,它是一個必要的邪惡

+1

沒有專業的開發者對錯誤繼續下一步「在任何情況下將永遠使用。一旦你使用這個所有的錯誤都被簡單地忽略.....粗略地翻譯它的意思是'我有一個錯誤,我不在乎'...... – Monty 2016-03-15 21:08:58