Re: Error Handling - IADS bug ? by Alan
Alan
Wed Jun 13 04:02:04 CDT 2007
Hey Michael, you are not only sharp eyed but brilliant, I looked at
that statement all day and couldn't see anything wrong.
I did in fact have option explicit at the very top of the script - I
have always assumed that this is a 'public' option and so is respected
by subs/functions - am I correct in this?
I'll look further at your suggestions.
Thanks again.
Alan
On Tue, 12 Jun 2007 16:47:13 -0700, "Michael Harris \(MVP\)"
<mikhar.at.mvps.dot.org> wrote:
>Alan Morris wrote:
>> I have a script to create users.
>>
>> .......
>> On Error Resume Next
>>
>> set objUser = objCIADS.Create("User", "CN=" & strFname & " "
>> &strLname)
>> objUser.Put "samAccountName", strAccName
>> objUser.SetInfo
>> if (Err.Number<>0) then
>> Wscript.Echo "User Creation failed for " & strFname & " " &
>> strLname & vbcrdlf & _
>
>If you also have 'Option Explicit' in effect (a best practice) then the line
>above will throw a runtime error if you *really* coded vbcrdlf when you
>meant vbCrLf. The Wscript.Echo fails silently because the string
>concatenation fails due to an undeclared variable name...
>
>Errors in error checking code in VBScript are problemmatic. If you use 'On
>Error GoTo 0' then the Err object is implicitly cleared and tere goes the
>evidence.
>
>Here's a suggested errorTrap technique that let's do you 'On Error GoTo 0'
>without losing evidence...
>
>Dim e 'as a global scope variable...
>
>On Error Resume Next
> x = 1/0 'forced divide by zero error...
>errorTrap:On Error goto 0
>
>wscript.echo "Error number:", e.number
>wscript.echo "Error number:", e 'same as above via Number default
>property...
>wscript.echo " description:", e.description
>wscript.echo " source:", e.source
>
>'-----------------------------------------------------------
>
>Sub errorTrap()
> Set e = new cErrState
>End Sub
>
>Class cErrState
> Private m_number
> Public description, source
> Sub Class_initialize
> with Err
> m_number = .number
> description = .description
> source = .source
> End with
> End Sub
> Public default Property Get Number()
> Number = m_number
> End Property
>End Class