Hi,
I seem to have hit a bug in the error handling of VBScript. I ran into the
problem when trying to assign a user to a group, but the group doesnâ??t exist.
If Error Handling is not turned on, then the script crashes on the GetObject
statement as expected. However, I want to catch and log the error, and
continue on with the script. So, I used the normal code:
Set objGroup = GetObject(â??LDAP://cn=Sales,cn=Users,dc=fabrikam,dc=comâ??)
If Err.Number <> 0 Then
WScript.Echo â??Error â?? unable to bind to groupâ??
Else
WScript.Echo â??Group boundâ??
End If
In a main routine, this code behaves exactly as expected. However, if I put
this code into a Function, like so:
On Error Resume Next
If SetGroup() Then
WScript.Echo "Set Group Fine"
Else
WScript.Echo "Set Group Failed"
End If
Function SetGroup()
SetGroup = FALSE
set objGroup = GetObject("LDAP://cn=Sales,cn=Users,dc=fabrikam,dc=com")
WScript.Echo "Did we get to this line?"
If Err.Number <> 0 Then
WScript.Echo "Error"
SetGroup = FALSE
Else
WScript.Echo "Fine"
SetGroup = TRUE
End If
End Function ' Set Group
The Error Handling code no longer runs. Instead the Script drops out of the
function back to the main routine. The WScript.Echo directly after the
GetObject was added to verify that the problem was on the GetObject line, not
with the Error Handling afterwards. In this simplified sample program, the
program does not crash at the GetObject (though it will, as expected, without
the On Error Resume Next), instead it simply drops back to the main routine.
In this particular case, reporting â??Set Group Fineâ??.
The full script where I ran into this problem is much more complicated, and
the GetObject is in nested routines. The main routine calls a subroutine,
which calls another subroutine, which calls the function that contains the
GetObject statement. In this complex program, the script drops all the way
back to the main routine, not just out of the Function.
So far, I have not found any code examples for testing for the existence of
a Group the way I can test for the existence of a file. With the GetObjectâ??s
incorrect behavior when an error occurs Iâ??m not even sure how such a test can
be written and incorporated into real-world scripts, as such error handling
belongs in a Function or Subroutine. In most complex scripts you probably
wouldnâ??t even know the data you are looking for until you were into a
subroutine or function.
Any help would be greatly appreciated.
Jim Winner
Program Manager
The Catholic University of America