Today, we were required to write a script to gather some information from Windows client systems. The script seemed to work fine till the "Quality Control" ghost took control.. Obviously..it was me who suggested this catastrophic idea of having "error handling" mechanism.
VbScript, by defaul throws any error during execution as run time error.. This is a controllable behaviour though..
The two important statements are
On Error Resume Next
On Error Goto 0
The first one enables the error handling mechanism.. ie you will not be getting run time errors as earlier.. So you need to handle it manually..
And the second one disables error handling mechanisms.. any code error = run time error..
Now how to work with error handling
Err.Number will give you the error number.. and more down here
On Error Resume Next
strComputer = "fictional"
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
If Err.Number <> 0 Then
WScript.Echo "Error: " & Err.Number
WScript.Echo "Error (Hex): " & Hex(Err.Number)
WScript.Echo "Source: " & Err.Source
WScript.Echo "Description: " & Err.Description
Err.Clear
End If
http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop1205.mspx#EMC
All that theory is just fine.. However, the script which has grown to around 600+ lines made us go mad.. One specific module to modify a registry hive wont work..
And finally we found where we went wrong..
"An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine."
http://msdn.microsoft.com/en-us/library/aa266173(VS.60).aspx
We had around 5 to 6 functions in the script. We added the "On Error Resume Next" stuff on all those and we were back on track after that.. A lesson learned in the hardway.. Long live the VbScript Error Handling.
Cheers !!
Sree