VBScript has no notion of throwing or catching exceptions, but the runtime provides a global Err object that contains the reuslts of the last operation performed. You have to explicitly check whether the Err.Number property is non-zero after each operation.


On Error Resume Next


If Err.Number <> 0 Then
  WScript.Echo "Error in DoStep1: " & Err.Description
End If


If Err.Number <> 0 Then
  WScript.Echo "Error in DoStop2:" & Err.Description
End If

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
         If Len(txtDenominator.Value) = 0 Then
            strErrorResultText = "Missing!"
            strErrorResultText = "'" & txtDenominator.Value & "'"
         End If
         strErrorMessage = "Error: " & Err.Number & _
            vbCrLf & vbCrLf & "The value you entered in the " _
            & "text box was: " & strErrorResultText
      Case Else
         strErrorMessage = "Error: " & Err.Number & _
            vbCrLf & vbCrLf & "Unrecognized error!"
   End Select
   MsgBox strErrorMessage, CRITICAL_ERROR   MSGBOX_OKONLY, _
      "Error Type = " & Err.Description 
End If