You are here: Scripting Techniques > Common Script Operations > Handling error conditions in a script

Handling error conditions in a script

When writing scripts, you need to be aware that some operations may fail. Fortunately, you can use the built-in error handling features of VBScript to take the appropriate action.

When working with collections, a common error is attempting to access an element that does not exist. Consider the following standalone script, which tries to assign a bogus keyword to a variable:

Dim k

Set k = Keywords("@#!%$")

Print TypeName(k)

Running this script produces the following error message:

Script error on line 2, column 1:

Keyword not found: '@#!%$'

Since all script errors are fatal, the Print statement never executes. However, if an On Error statement is inserted before line 2, the script executes in its entirety:

Dim k

On Error Resume Next

Set k = Keywords("@#!%$")

Print TypeName(k)

This time, the script prints the word Empty to the output window, since the variable k is still uninitialized. Technically, the Set statement still results in an error, but the On Error directive causes execution to continue with the statement immediately following the one that caused the error.

Another common error is attempting to add an element to a collection that already exists. Consider the following standalone script, which adds a new group to the project:

Groups.Add "qwerty"

The first time this script is run, it executes silently. However, running it again produces the following error message:

Script error on line 1, column 1:

Group already exists: 'qwerty'

Again, the solution is to use the On Error statement:

On Error Resume Next

Groups.Add "qwerty"

VBScript also provides an Err object that you can use to determine whether the last statement was successful. The following example demonstrates how to use the Err object to write code that always operates on a specified member of a collection, whether or not it already exists:

Dim k

On Error Resume Next

Set k = Keywords("primary")

If Err <> 0 Then ' keyword not found

    Set k = Keywords.Add("primary")

    Err.Clear

End If

' Add a secondary keyword under "primary"

Keywords.Add "secondary", k

The Set statement in the third line attempts to access a specific keyword. If it exists, the default property of the Err object returns zero, and execution continues with the last line of the script. If the keyword does not exist, the script creates it using the Add method. In either case, when the last line of the script executes, the variable k contains the appropriate object.

Finally, note that the scope of the On Error statement is limited to the procedure in which it appears. Consider the following function, which resides in a global script module:

Function GetKeyword(Name)

    On Error Resume Next

    Set GetKeyword = Keywords(Name)

    If Err <> 0 Then ' keyword not found

        Set GetKeyword = Keywords.Add(Name)

        Err.Clear

    End If

End Function

The GetKeyword function generalizes the technique employed in the previous example. It accepts a string and unconditionally returns a top-level keyword having the same name. If necessary, the function creates a new keyword; otherwise, it returns an existing one. The following example demonstrates how to call GetKeyword from another script:

Dim k

Set k = GetKeyword("primary")

Keywords.Add "secondary", k

If the specified secondary keyword already exists, the third line results in an unhandled error, even though the GetKeyword function uses the On Error statement. In order to trap all errors, an additional On Error statement is required:

Dim k

Set k = GetKeyword("primary")

On Error Resume Next

Keywords.Add "secondary", k