1
votes

While using On Error Resume Next,it skips the encountered error and move to the next line. If we use err.Number and err.Description it shows message and number related to the error. My question is: what if it faces more than on error...then how it will show?

On Error Resume Next
    intDivideByZero
    floatDivideByZero
If err.Number <> 0 Then
    msgbox "the error number is : " & err.Number
    msgbox "the error description is : " & err.Description
End If
On error Resume 0
1
It should be On Error Goto 0 not On Error Resume 0, that will trigger a Syntax Error which you are not expecting. - user692942

1 Answers

3
votes

VBScript error handling is rather limited. You will need to put an error handler after each line where an error can occur. Also, the Err object isn't automatically reset after an error, so you need to do that yourself. Otherwise the object will still indicate an error after the next statement, even if none occured there.

On Error Resume Next
intDivideByZero
If Err Then
    WScript.Echo "0x" & Hex(Err.Number) & ": " & Err.Description
    Err.Clear
End If
floatDivideByZero
If Err Then
    WScript.Echo "0x" & Hex(Err.Number) & ": " & Err.Description
    Err.Clear
End If
On Error Goto 0

You can simplify that a little bit by wrapping the handler in a procedure and calling that procedure after each statement:

Sub Trap
    If Err Then
        WScript.Echo "0x" & Hex(Err.Number) & ": " & Err.Description
        Err.Clear
    End If
End Sub

On Error Resume Next
intDivideByZero   : Trap
floatDivideByZero : Trap
On Error Goto 0