views:

5848

answers:

4

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

A: 

For error handling in VBScript is used "On Error" clausule. There are 3 ways, how to handle errors:

  • On Error Resume Next '' ignore errors
  • On Error GoTo 0 '' removes error handlning
  • On Error GoTo HandleError '' on error will code jump to specified signal

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
TcKs
Thee is no On Error GoTo Handler feature in VBScript nor a Goto in VBScript
AnthonyWJones
+3  A: 

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.

jammus
A: 

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

A: 

hi, can any one suggest ,,, got statement is not working in my system.

nitesh
Welcome to Stack Overflow! Please use the **Ask Question** button in the top right to post new questions; do not post questions as answers/comments to existing posts.
Helen