Re: Error handling bug on GetObject?
- From: James Winner <JamesWinner@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 12 Jul 2006 04:06:02 -0700
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
- References:
- Error handling bug on GetObject?
- From: James Winner
- Re: Error handling bug on GetObject?
- From: Tom Lavedas
- Error handling bug on GetObject?
- Prev by Date: Re: rename a folder full of jpgs with the size of the file
- Next by Date: Getting window handles...
- Previous by thread: Re: Error handling bug on GetObject?
- Next by thread: Re: Error handling bug on GetObject?
- Index(es):
Relevant Pages
|