[Previous] [Table of Contents] [Next]

Run-Time Errors

Both VBScript and JScript support inline error handling, meaning that your script can intercept and recover from errors that otherwise would cause the script to terminate. In this section, I'll summarize what you need to know about using explicit run-time error handling within scripts.

Handling Run-Time Errors in VBScript

You implement explicit run-time error handling in VBScript by using the On Error Resume Next statement, which causes the next statement to be executed after a run-time error. This statement enables inline run-time error handling. (Only syntax errors are still reported by the script engine.)

You can retrieve the error code by using the Err object. For example, if oWSH is an object variable pointing to the WshShell object, you can check a possible error raised while using the RegRead method:

valx = oWSH.RegRead("....", "xxxx")
If Err <> 0 Then

If a run-time error occurs during execution of the first statement, the value of the Err object is not equal to 0. You can retrieve the error code by using Err.Number and retrieve the error text by using Err.Description. I used this technique in several earlier chapters to handle run-time errors.

You use the On Error GoTo 0 statement to disable run-time error handling. After this statement, the script engine handles run-time errors.

As I mentioned, enabling inline run-time error handling suppresses all implicit error messages, so your script is responsible for detecting and reporting run-time errors. The danger with this approach is that certain hard-to-find errors could escape detection. Even if the script contains an Option Explicit statement, mistyped variables and function or procedure names are not reported as erroneous. Instead, the statement simply fails and the script engine moves on to the next statement. To avoid this risk, I strongly recommend that you disable run-time error handling (using On Error GoTo 0) as soon as possible after enabling it.

Handling Run-Time Errors in JScript

JScript supports run-time error handling in script engine versions 5 and later. You use the try {…} catch (e) {…} sequence as follows:

    var valx = WSH.RegRead("....", "xxxx");
catch (e)
    if (e != 0)
        WScript.Echo("Error during Registry access");

You must set the try keyword in front of a statement. You enclose the statement or block of statements in braces. The catch (e) statement is called if a run-time error occurs. The variable e receives the error object, which can be evaluated in the statements following the catch block. (These statements must also be enclosed in braces.)

Raising a Run-Time Error in VBScript

You can raise your own run-time errors by using the methods the script language provides. In VBScript, you use the Raise method of the Err object. The following code raises a run-time error and displays the error description:

On Error Resume Next

Err.Raise 6    ' Raise an error.

MsgBox "Error code: " & Err.Number & vbCrLf & Err.Description, _
       vbOKOnly, "VBScript-Error-Description"

On Error GoTo 0

This code simulates an overflow error (code 6). You can use this snippet to write a short VBScript program that asks for an error code and returns the error description.

The VBScriptError.vbs file in the \WSHDevGuide\Chapter14 folder on the book's companion CD asks for an error number and shows the description of the error. All error messages are also described in the VBScript Language Reference. But the same can't be said for errors caused by the operating system (or OLE or COM components). You can obtain these error descriptions from internal tables by using the right Windows API calls, but these techniques are beyond the scope of this book.