Checking for Errors in VBScript

It is worthwhile to look at error handling in a little more detail now. Normally errors that occur in a script are termed fatal errors. This means that execution of the script terminates whenever an error occurs. When this happens, a dialog box opens and gives you the unique number and description of the error. While this is useful, sometimes you may like to set errors to be nonfatal, so that execution continues after the error. To do this, you include the following line in your code:

    On Error Resume Next

Once you have done this, any line with an error is ignored. This can cause confusion, as can be seen from the following code. Note the missing P in LDAP:

    On Error Resume Next

    Set objGroup = GetObject("LDA://cn=Managers,ou=Sales,dc=mycorp,dc=com")

    objGroup.GetInfo
    WScript.Echo objGroup.Description
    objGroup.Description = "My new group description goes here"
    objGroup.GetInfo
    WScript.Echo objGroup.Description

This script fails to execute any of the lines after the On Error Resume Next statement, as the first LDAP call into the objGroup variable failed. However, it will not terminate as usual with an error after the GetObject line, due to the On Error statement. To get around this, you should add a couple lines to do error checking . Example 21-9 is a good example of error checking in a different script.

Example 21-9. Error checking in VBScript

On Error Resume Next '********************************************************************** 'Clear errors '********************************************************************** ...

Get Active Directory, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.