除了DMasons回答,microsoft broke down the removal of "On Error Goto X" command and it's purpose.不幸的是,與許多語言(JavaScript的,C# )具有非常方便和方便的「嘗試{}和捕獲{}」參數,vbscript不提供廣泛的處理錯誤細分。是的,On error resume next命令在其簡單的目的上是有效的,但還有其他方法可以處理我認爲有用的錯誤。
例如,您可以創建故障代碼更容易使用此代碼:
Troubleshootvbs.vbs
Const Troubleshoot_this_code = "C:\testenv\testfail.vbs"
Const Place_Results_In = "C:\testenv\troubleshoot.log"
Dim filesystemobject: Set filesystemobject = CreateObject("Scripting.FileSystemObject")
Dim filetotroubleshoot: Set filetotroubleshoot = filesystemobject.OpenTextFile(Troubleshoot_this_code, 1, True)
Dim Troubleshoot_Lines : Troubleshoot_Lines = split(filetotroubleshoot.ReadAll, vbcrlf)
Dim tbl : Set tbl = filesystemobject.OpenTextFile(Place_Results_In, 8, True)
Dim InsertErrorHandling(),IEH,line : Redim InsertErrorHandling(1) : IEH = 1
InsertErrorHandling(0) = "On Error Resume Next"
For each line in Troubleshoot_Lines
dim errclause : errclause = "if err.number<>0 then : return=" & chr(34) & "Error Found on Line[" & chr(34) & " & IEH & " & chr(34) & "]:" & chr(34) & " & err.number & " & chr(34) & ":" & chr(34) & " & err.description & vbcrlf : err.clear : else : return = IEH & " & chr(34) & ": Clear" & chr(34) & " & vbcrlf : end if : tbl.Writeline return : IEH = IEH + 1"
InsertErrorHandling(ubound(inserterrorhandling)) = line & vbcrlf & errclause
Redim Preserve InsertErrorHandling(Ubound(InsertErrorHandling)+1)
Next
dim FullTestCode : FullTestCode = Join(InsertErrorHandling, vbcrlf)
ExecuteGlobal FullTestCode
tbl.close
樣品:TestFail.vbs
dim query : query = CreateObject("Scripting.FileSystemObect")
dim result : result = query.CreateTextFile("test.txt")
dim fail : fail = result.readall
示例:疑難解答.log
Error Found on Line[1]:429:ActiveX component can't create object
Error Found on Line[2]:424:Object required
Error Found on Line[3]:424:Object required
VBScript不支持通過繼續執行標籤來執行錯誤處理的概念。換句話說,你不能在VBScript中使用'On Error GoTo'。相反,使用「On Error Resume Next」,然後檢查Errors集合的「Err.Number」和「Count」屬性。問題不是你要找的答案。 :(http://msdn.microsoft.com/en-us/library/windows/desktop/ms675540(v=vs.85).aspx – DMason
「不」是一個完全可以接受的答案,當你說爲什麼:-) –
爲什麼你不要繼續提交這個答案。 –