0
votes

I've inherited a big ugly mess of code written in several languages. The part of the mess that's written in C# seems to be set up to log most uncaught exceptions in a log file. There is another part of the program in VBScript that doesn't log errors.

Is there some simple way of making the VBScript code catch all uncaught errors and log them somewhere? I think that the answer may be "no" because VBScript doesn't have try-catch, having "On Error Resume Next" instead.

Edit: I am well-aware of the fact that error handling in VBScript is completely different from VB.net. I was just wondering if there is some trick/hack that will allow me to handle errors that are otherwise unhandled.

3
VBScript does not support the concept of error handling by continuing execution at a label. In other words, you cannot use On Error GoTo in VBScript. Instead, use On Error Resume Next and then check both Err.Number and the Count property of the Errors collection. Prob not the answer you were looking for. :( msdn.microsoft.com/en-us/library/windows/desktop/… - Dave Mason
"No" is a perfectly acceptable answer when you say why :-) - Vivian River
Why don't you go ahead and submit that as an answer. - Vivian River

3 Answers

1
votes

VBScript does not support the concept of error handling by continuing execution at a label. In other words, you cannot use On Error GoTo in VBScript. Instead, use On Error Resume Next and then check both Err.Number and the Count property of the Errors collection.

Handling Errors in VBScript

Something else that may help uncover VBScript errors is the use of Option Explicit. It forces the explicit declaration of all variables and prevents the accidental reuse of the name of a previously declared variable. Also, if you mistype a declared variable's name or try to use an undeclared variable, an error message is generated.

0
votes

You can do it several ways, but as a quick example, this is how you can handle basic error handling:

On Error Resume Next

'Insert code to do what you need

If Err.Number <> 0 Then
  WScript.Echo "Error in executing script: " & Err.Number & " - " & Err.Description
  Err.Clear
End If

'Stop doing error handling    
On Error Goto 0
0
votes

In addition to DMasons answer, microsoft broke down the removal of "On Error Goto X" command and it's purpose. Unfortunately, unlike many languages (javascript, C#) that have the extremely handy and convenient "Try{} and Catch{}" parameters, vbscript does not offer an extensive break down of handling errors. Yes the On error resume next command is efficient at it's simplistic purpose, but there are other methods for handling errors which i've found useful.

For instance, you can create troubleshooting code easier with this code:

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

Sample: TestFail.vbs

dim query : query = CreateObject("Scripting.FileSystemObect")
dim result : result = query.CreateTextFile("test.txt")
dim fail : fail = result.readall

Sample: Troubleshoot.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