Turning Off Error Handling as a Debugging Tool

Microsoft® Windows® 2000 Scripting Guide

The primary advantage of error handling is that it allows a script to proceed from start to finish without displaying any error messages, and without abruptly terminating if an error condition occurs. At the same time, the primary disadvantage of error handling is that error handling allows a script to proceed from start to finish without displaying any error messages, and without abruptly terminating if an error condition occurs. After all, without seeing an error message, you will not know where the error occurred in your script. In fact, you might not even know that an error did occur.

Because of this, it is often a good idea to turn off error handling when writing and debugging a script. For example, the following script sample creates a new organizational unit and then adds a user to it. If the script is run by a user who does not have the right to create a new organizational unit, nothing happens: neither the new organizational unit nor the new user is created, nor does an error message appear.

On Error Resume Next
Set objRootDSE = GetObject("LDAP://RootDSE")
ObjDomain = rootDSE.Get("DefaultNamingContext")
strOUName = "Accounting"
Set objContainer = GetObject("LDAP://" & objDomain)
Set objOU = objContainer.Create("organizationalUnit", "OU=" & strOUName)
objOU.SetInfo
strUserAccountName = "rsmith"
strSAMAccountName = "rsmith"
set objUser = objOU.Create("user", "cn=" & strUserAccountName )
objUser.Put "samAccountName", strSAMAccountName
objUser.SetInfo

This script is difficult to troubleshoot because it is syntactically correct; under the right circumstances, this script works every time. In this case, the script is correct; the failure occurs not because of a defect in the code, but because the user is not authorized to create a new organizational unit. This problem can be very difficult to identify; when a script fails to work, the usual assumption is that there is something wrong with the script code.

To catch run-time errors such as this, comment out the On Error Resume Next statement. When you run the script without error handling enabled, you immediately receive an error message such as the following:

C:\Scripts\NewOU.vbs(6, 1) Microsoft VBScript runtime error: Object required: 'rootDSE'

This error message tells you that the script failed because it could not return a reference to the rootDSE object. This means that either the domain does not exist or the user is not authorized to bind to the domain root. With error handling enabled, this problem is more difficult to diagnose.