Function The Debug object is an intrinsic global object that can send an output to a script debugger, such as the Microsoft Script Debugger.
Remarks The Debug object cannot be created directly, but it is always available for use.
The Write and WriteLine methods of the Debug object display strings in the Immediate window of the Microsoft Script Debugger at run time. If the script is not being debugged, the methods have no effect.2
Method Write
Description Sends strings to the script debugger Usage Debug.Write ([str1 [,str2 [, …[, strN]]]] Arguments str1...strN
Optional. Strings to send to the script debugger
Remarks The Write method sends strings to the Immediate window of the Microsoft Script Debugger at run time. If the script is not being debugged, the Write method has no effect.
The Write method is almost identical to the WriteLine method. The only difference is that the WriteLine method sends a newline character after the strings are sent.
Example Dim counter Counter = 30
Debug.Write “The value of counter is “ & counter Method WriteLine
Description Sends strings to the script debugger, followed by the newline character Usage Debug.WriteLine ([str1 [,str2 [, …[, strN]]]]
Arguments str1...strN
Optional. Strings to send to the script debugger
Remarks The WriteLine method sends strings to the Immediate window of the Microsoft Script Debugger at run time. If the script is not being debugged, the WriteLine method has no effect.
The WriteLine method is almost identical to the Write method. The only difference is that the Write method does not send a newline character after the strings are sent.
Example Dim counter Counter = 30
Debug.Write “The value of counter is “ & counter
Err
Function Contains information about the last run-time error. Accepts the Raise and Clear methods for generating and clearing run-time errors.
Usage val = Err.Property Err.Method
Arguments Varies with properties and methods used (see below)
Remarks The Err object is an intrinsic object with global scope — there is no need to create an instance of it in your code. The properties of the Err object are set by the generator of an error — VBScript, an Automation object, or the VBScript programmer.
The default property of the Err object is Number. Err.Number contains an integer and can be used by an Automation object to return an SCODE.
When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and information that can be used to handle it. To generate a run-time error in your code, use the Raise method.
The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement. The Clear method can be used to explicitly reset Err.
For more information, see Microsoft Web site.3 Property Description
Function: Returns or sets a descriptive string associated with an error. Usage: Err.Description [= stringexpression]
Arguments stringexpression
A string expression containing a description of the error.
Remarks: The Description property consists of a short description of the error. Use this property to alert the user to an error that you can't or don't want to handle. When generating a user-defined error, assign a short description of your error to this property. If Description isn't filled in, and the value of Number corresponds to a VBScript run-time error, the descriptive string associated with the error is returned
Example: On Error Resume Next
Error.Raise 39 ‘This is a non-defined VBScript error Err.Description =”Pump OverFlow” ‘Define the error message
MsgBox “Error type is “ & Err.Description Property HelpContext
Function: Sets or returns a context ID for a topic in a Help File. Usage: Err.HelpContext [= contextID]
Arguments contextID
Optional. A valid identifier for a Help topic within the Help file.
Remarks: If a Help file is specified in HelpFile, the HelpContext property is used to automatically display the Help topic identified. If both HelpFile and HelpContext are empty, the value of the Number property is checked. If it corresponds to a VBScript run-time error value, then the VBScript Help context ID for the error is used. If the Number property doesn't correspond to a VBScript error, the contents screen for the VBScript Help file is displayed.
Example: On Error Resume Next Const usercontextID = 10
Error.Raise 48 ‘Error Loading DLL
Err.HelpFile = “myDLL.hlp” ‘The help file
Err.HelpContext = usercontextID ‘Specify the user context ID If Err.Number <> 0 Then
MsgBox “Press F1 for help ” & “Error:” & Error.Description &_ Err.Helpfile & Err.HelpContext
End If
3http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop1205.mspx
Property HelpFile
Function: Sets or returns a fully qualified path to a Help File Usage: Err.HelpFile [= contextID]
Arguments contextID
Optional. Fully qualified path to the Help file
Remarks: If a Help file is specified in HelpFile, it is automatically called when the user clicks the Help button (or presses the F1 key) in the error message dialog box. If the HelpContext property contains a valid context ID for the specified file, that topic is automatically displayed. If no HelpFile is specified, the VBScript Help file is displayed.
Example On Error Resume Next
Err.Raise 11 ‘Divide by 0 error
Err.HelpFile = “myHelpFile.hlp” Err.HelpContext = usercontextID If Err.Number <>0 Then
MsgBox “Press F1 for help” & vbCrLf & “Error: “ &Err.Description _ & Error.HelpFile & Err.HelpContext
End If Property Number
Function: Returns or sets a numeric value specifying an error. Number is the Err object's default property
Usage: Err. Number [= errornumber] Arguments errornumber
An integer representing a VBScript error number or an SCODE error value Remarks: When returning a user-defined error from an Automation object, set Err.Number
by adding the number you selected as an error code to the constant vbObjectError.
Example On Error Resume Next
Err.Raise 11 ‘Divide by 0 error
Err.HelpFile = “myHelpFile.hlp” Err.HelpContext = usercontextID If Err.Number <>0 Then
MsgBox “Press F1 for help” & vbCrLf & “Error: “ &Err.Description _ & Error.HelpFile & Err.HelpContext
End If Property Source
Function: Returns or sets the name of the object or application that originally generated the error.
Usage: Err.Source [= stringexpression] Arguments stringexpression
A string expression representing the application that generated the error
Remarks: The Source property specifies a string expression that is usually the class name or programmatic ID of the object that caused the error. Use Source to provide your users with information when your code is unable to handle an error generated in an accessed object. For example, if you access Microsoft Excel and it generates a Division by zero error, Microsoft Excel sets Err.Number to its error code for that error and sets Source to Excel.Application. Note that if the error is generated in another object called by Microsoft Excel, Excel intercepts the error and sets Err.Number to its own code for Division by zero. However, it leaves the other Err object (including Source) as set by the object that generated the error. Source always contains the name of the object that originally generated the error — your code can try to handle the error according to the error documentation of the object you accessed. If your error handler fails, you can use the Err object information to describe the error to your user, using Source