| || |
Error Handling in VBScript
I have been trying since quite a few days to optimize my framework to handle specific exceptions in VBscript.
However, I am facing 1 issue: Check the code below.
I am calling a Function named Function_A from a main Sub called Temp.
This function returns an Array which I am capturing in ReturnArray.
Now, I have deliberately put an error in the function at the given place in code.
This statement will result in an error. Due to this, the function will not get executed completely and will return a NULL object.
Now going back to the Temp Sub, the IF condition will result in showing PASS in the logs because of the "On Error Resume Next".
Any inputs/suggestions on how can I effectively use the "On Error Resume Next" - So that :
1) I get the Error Exception (where it may have occurred).
2) The IF condition should logically "FAIL".
Secondly, would like to know how advisable is it to use "On Error..." at all Subroutines or Functions or whether it should be used at only specific code blocks (where the probability of getting an error is high).
<font class="small">Code:</font><hr /><pre>
On Error Resume Next
Name = "ABCD"
ReturnArray = Function_A(Name)
'This statement will throw a TypeMismatch Error and will goto next statement
'due to "On Error Resume Next" and will print "PASS"
if IsArray(ReturnArray) AND ReturnArray(0) = "A" then
Dim objBrowser, objBrowser2
set objBrowser = Sys.Process("iexplore")
'This statement will result in an Error
set objBrowser2 = strbrowser
if Name = "ABCD" then
ReturnArray = Array("A", "B", True)
log.Message "Inside IF"
ReturnArray = Array("", "", False)
log.Message "Inside ELSE"
Function1 = ReturnArray
Re: Error Handling in VBScript
See this TC forum thread.