DekGenius.com
[ Team LiB ] Previous Section Next Section

19.3 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 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
'**********************************************************************
Err.Clear
   
'**********************************************************************
'Get a pointer to the Administrator account
'**********************************************************************
Set objUser = GetObject ("LDAP://cn=Administrator,cn=Users,dc=mycorp,dc=com")
If Hex(Err.Number)="&H80005000" Then
  WScript.Echo "Bad ADSI path!" & vbCrLf & "Err. Number: " _
    & vbTab & CStr(Hex(Err.Number)) & vbCrLf & "Err. Descr.: " _
    & vbTab & Err.Description
  WScript.Quit
End If
   
'**********************************************************************
'Explicitly call GetInfo for completeness
'**********************************************************************
objUser.GetInfo
   
'**********************************************************************
'Clear any previous errors
'**********************************************************************
Err.Clear
   
'**********************************************************************
'Try and get a pointer to the "moose" attribute of the user (which
'doesn't exist)
'**********************************************************************
x = objUser.Get("moose")
   
'**********************************************************************
'Check for property does not exist error
'**********************************************************************
If Hex(Err.Number)="&H8000500D" Then
  WScript.Echo "No such property!" & vbCrLf & "Err. Number: " _
    & vbTab & CStr(Hex(Err.Number)) & vbCrLf & "Err. Descr.: " _
    & vbTab & Err.Description
End If

This is a simple example; the path does exist and the moose property does not exist for the user. ADSI errors start at 80005 in hexadecimal, and 8000500D is the error indicating that there is no such property. The &H prefix indicates that the following string is a hexadecimal number. You must use the Err::Clear method from the Err interface to clear any existing error information, prior to making a call that could generate an error. If an error has occurred, the value of Err.Number is nonzero; if Err.Number is 0, no error occurred. If an error has occurred, Err.Description contains any description that has been set for that error.

We use the functions Hex and CStr in the example one after the other to print out the hexadecimal string of the error number. We choose to do this because Microsoft specifies error numbers in hexadecimal, and if you are to look them up easily in Microsoft's documentation, you need to see the hexadecimal rather than getting out a calculator. The CStr function converts the newly converted hexadecimal value to a text string that can be printed out.

Since most calls to the Err interface will be to retrieve the Err::Number property, the Err::Number property is set as the default property method, meaning that you don't have to state it explicitly. For example, these two statements are equivalent:

If Hex(Err)="&H8000500D" Then
If Hex(Err.Number)="&H8000500D" Then

In addition, as Hex(0) is the same as 0, most sample code that you will see using VBScript looks like this:

On Error Resume Next
   
'Some_code_goes_here
   
Err.Clear
Set x = GetObject(something_goes_here)
If Err=0 Then
  'No error occurred
  Some_success_code_goes_here
Else
  'Error occurred
  Some_failure_code_goes_here
End If

Finally, to reset error checking back to the default as if the On Error Resume Next statement had not been included, we use the following code:

'The last character is a zero, not a capital "o"
On Error Goto 0

A full list of ADSI errors can be found in the MSDN library (http://msdn.microsoft.com/library/) under Networking and Directory Services Active Directory, Active Directory Service Interfaces and Directory Services SDK Documentation Directory Services Active Directory Service Interfaces Active Directory Service Interfaces Reference ADSI Error Codes.

    [ Team LiB ] Previous Section Next Section