除了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
“不”是一个完全可以接受的答案,当你说为什么:-) –
为什么你不要继续提交这个答案。 –