Add an error handler to a vbscript

I have some code here that I'd like to have an error handler for. This script works fine if the following registry key exists HKEY_LOCAL_MACHINE\SYSTEM\Setup\OEMDuplicatorString\
however if the registry key does not exist I get an error on Line 51 Char 1, The error is: Object is not a collection.

Is there anyway to throw an exception that does not cause the script to error but creates an entry in the log that says something like "Reg key does not exist"?

Dim objShell, objFileSystem
Dim regComputerName, ComputerName
Dim CurrentDate, CurrentTime
CurrentDate = Date
CurrentTime = Time

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
strValueName,arrValues

oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strValueName,arrValues

'create the FileSystemObject
Set fso = CreateObject("scripting.filesystemobject")
'Open the data file for appending
'fso.OpenTextFile "c:\reglog.txt", ForAppending, True, TristateFalse
Set logts = fso.OpenTextFile("c:\reglog.txt", 8, -1, 0)

regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control\" & _
"ComputerName\ComputerName\ComputerName"
Set objShell = CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
ComputerName = objShell.RegRead(regComputerName)

For Each strValue In arrValues
    'StdOut.WriteLine strValue
    'append to file
    logts.WriteLine ComputerName & " " & strValue & " " & "Recorded on " & CurrentDate & " " & "at " & CurrentTime
Next

'close the file
logts.Close
hpopsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AzraSoundCommented:
VBScript does not have very good error handling.  All you can do is add On Error Resume Next at the top of your script, and periodically check for errors after certain function calls you feel may cause an error, e.g.,


On Error Resume Next

...


Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
strValueName,arrValues

If Err.Number <> 0 Then
    'error occurred - take action here
Else
    'continue with rest of processing here
End If
0
hpopsAuthor Commented:
I have tried to make this work and am still getting a collection error when the file does not exist. I'm really lost on this one.
0
AzraSoundCommented:
Where is the error occurring?  What is Line 51 in your script?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

hpopsAuthor Commented:
"For Each strValue In arrValues" is line 51. Sorry I stripped off the comments and forgot it would make a difference once I posted "Line 51" here. It's actually line 33 going by my first post.
0
EDDYKTCommented:
You can use isarray to test


if (isarray(arrValues)) then
For Each strValue In arrValues
   'StdOut.WriteLine strValue
   'append to file
   logts.WriteLine ComputerName & " " & strValue & " " & "Recorded on " & CurrentDate & " " & "at " & CurrentTime
Next
else

logts.WriteLine ComputerName & " " & arrValues & " " & "Recorded on " & CurrentDate & " " & "at " & CurrentTime
end if
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AzraSoundCommented:
It should be glossing over that error, though, if you have added On Error Resume Next.  However, after the For...Next loop you should be able to test Err.Number and find that it does not equal 0.
0
hpopsAuthor Commented:
Thanks everyone for helping, however my problem was resolved once I added the array to my existing code that EDDYKT mentioned above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.