I am having an VB Script. I need to log the error information in a file. I need to log every information like error number error description and in which sub routine does the error occured.
Please provide some code
I am having an VB Script. I need to log the error information in a file. I need to log every information like error number error description and in which sub routine does the error occured.
Please provide some code
For error handling in VBScript is used "On Error" clausule. There are 3 ways, how to handle errors:
Samples:
On Error Resume Next '' ignore errors
SomeIgnorableFunction()
On Error GoTo 0 '' removes error ignoring
SomeImportantFunction()
On Error GoTo HandleError '' on error will code jump to specified signal
Dim a
a = 15 / 0
GoTo Finish '' skips error handling
HandleError:
Dim msg
Set msg = Err.Description & vbCrLf & Err.Number
MsgBox msg
Finish:
'' there is end of sciprt
You can make use of the FileSystemObject and the Error object if you're using VBScript.
Paste the following into error.vbs and run it. It will throw an error then log the details to a file called c:\errors.log
Option Explicit
On Error Resume Next ' Potential error coming up
Dim MyArray(5)
MyArray(7) = "BWA HA HA"
If Err.Number <> 0 Then
LogError(Err)
Err.Clear
End If
On Error Goto 0 ' Stop looking for errors
Sub LogError(Details)
Dim fs : Set fs = CreateObject("Scripting.FileSystemObject")
Dim logFile : Set logFile = fs.OpenTextFile("c:\errors.log", 8, True)
logFile.WriteLine(Now() & ": Error: " & Details.Number & " Details: " & Details.Description)
End Sub
If you're using an ASP page then you can make use of ASPError to get more detailed information about the error such as line number, etc (remember to replace CreateObject with Server.CreateObject).
Edit: To get the line number that caused the error in .vbs script you could add this as a parameter to the sub routine.
VBScript doesn't support on error goto label. The following piece of code will never work -:
On Error GoTo HandleError '' on error will code jump to specified signal
Dim aa = 15 / 0
GoTo Finish '' skips error
handlingHandleError:
Dim msgSet
msg = Err.Description & vbCrLf & Err.Number
MsgBox msgFinish:'' there is end of sciprt