Re: Error handling bug on GetObject?



Thanks. I had missed that aspect of error trapping. I'll test to see if
that is what is happening here. It would still represent inconsistent
behavior, in that my scripts rely heavily on error trapping and this is the
only time I've run into this. But I'll do some sample scripts to see if
there is a consistent pattern where error trapping works globally and where
it doesn't.

Thanks again!

Jim W.

"Tom Lavedas" wrote:

I believe the documentation covers this (but you have to read
carefully) and it's not a bug. The issue is the context of the error
handler. That is, the error handler you invoke at the top level of
your procedure is Not global to another routine called from it. You
need to invoke error handling specific to each function of subroutine
you might call from your main procedure.

Tom Lavedas
============
http://members.cox.net/tglbatch/wsh

James Winner wrote:
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


.



Relevant Pages

  • Re: Error handling bug on GetObject?
    ... I seem to have hit a bug in the error handling of VBScript. ... If Error Handling is not turned on, then the script crashes on the GetObject ... In a main routine, this code behaves exactly as expected. ...
    (microsoft.public.scripting.vbscript)
  • Error handling bug on GetObject?
    ... If Error Handling is not turned on, then the script crashes on the GetObject ... In a main routine, this code behaves exactly as expected. ...
    (microsoft.public.scripting.vbscript)
  • Re: Thoughts on current error handling best practice with VBScript
    ... Anthony Jones also recommends not using VBS if you're concerned about ... Dim sVal ... impression about how much of an afterthought error handling has been ... you've run the script from an open command window, using cscript, Ctrl- ...
    (microsoft.public.scripting.vbscript)
  • Re: Thoughts on current error handling best practice with VBScript
    ... It isn't really reasonable to expect a 'casual programmer' to program ... Taking a casual script that includes no error handling and fixing it ... if your code attempts to create a file in a non-existent folder, ...
    (microsoft.public.scripting.vbscript)
  • Re: Automated changing of local admin password on workstations?
    ... Funny coincidence....I just built a script yesterday for that exact same ... ' Create the file system object to handle input file operations ... 'Obtain the administrative account name from the user ... ' Error handling to trap lazy users inputting a null account name ...
    (microsoft.public.windows.group_policy)