Handling VBScript Run-Time Errors

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Although Microsoft® Visual Basic® Scripting Edition (VBScript) provides the Err object, and that object exposes the same methods and properties available in the VBA Err object, writing error handlers using VBScript is not the same as in Visual Basic for Applications (VBA). The primary limitation is due to the limited functionality of the On Error statement in VBScript. In VBScript, you cannot branch to an error handler by using the familiar On Error GoTo ErrorHandler syntax. You can only enable error handling in VBScript by using the On Error Resume Next syntax.

The following code excerpt shows the error-handler portion of a script. The script performs simple division and then immediately checks to see if an error occurred and responds accordingly:

intResult = intNumerator/intDenominator

' Check for errors as a result of the division.
If Err <> 0 Then
   Select Case Err.Number
      Case DIVIDE_BY_ZERO
         If Len(txtDenominator.Value) = 0 Then
            strErrorResultText = "Missing!"
         Else
            strErrorResultText = "'" & txtDenominator.Value & "'"
         End If
         strErrorMessage = "Error: " & Err.Number & _
            vbCrLf & vbCrLf & "The value you entered in the " _
            & "text box was: " & strErrorResultText
         txtDenominator.Focus
      Case Else
         strErrorMessage = "Error: " & Err.Number & _
            vbCrLf & vbCrLf & "Unrecognized error!"
   End Select
   MsgBox strErrorMessage, CRITICAL_ERROR + MSGBOX_OKONLY, _
      "Error Type = " & Err.Description 
End If

See Also

Writing Error-Free Code | Design-Time Tools | Run-Time Tools | Script Debugging Tools | Basic Error Handling | Getting Information About an Error | Error Handling in Class Modules | Handling JScript Run-Time Errors