How to handle errors in VB Script

I have a VB Script. I need to write error information to a file. I need to write down every information, such as a description of the error of the error number, and in which the routine executes the error.

Please indicate code

+1
source share
4 answers

To handle errors in VBScript, the "About Error" clause is used. There are 3 ways to handle errors:

  • In the "Error while repeating" field, "ignore errors"
  • If GoTo 0 '' error occurs, error handling is deleted
  • On error GoTo HandleError '' on error, the code will jump to the specified signal

Examples:

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
-3
source

FileSystemObject , VBScript.

error.vbs . , 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

ASP, ASPError, , , .. ( CreateObject Server.CreateObject).

Edit: , .vbs script, .

+3

VBScript goto. -:

GoTo HandleError ''

Dim aa = 15/0

GoTo Finish ''

handlingHandleError:

Dim msgSet

msg = Err.Description vbCrLf Err.Number

MsgBox msgFinish: '' there is an end to sciprt

+2
source

Put the whole sub or function inside the do loop (or another loop). Put error handling on the outside of the do loop

private sub BucketList()
do while 1=1
  ClimbMountain(top)
    if err.Number <> 0 then exit do
  SwimOcean(deep)
    if err.Number <> 0 then exit do
  GiveErrorHandlingToVBS(isNeverGoingToHappen)
    if err.Number <> 0 then exit do

  exit do
loop

'Error Handler
if err.Number <> 0 then
  'handle error
end if

end sub
+1
source

All Articles