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.DescriptionThis 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 19-9 is a
good example of error checking in a different script.
Example 19-9. Error checking in VBScript
On Error Resume Next '********************************************************************** 'Clear errors '********************************************************************** ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access