Error Handling in VBScript

Originally posted by AutoBot_1 on September 07, 2011

Hi Folks…

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.

Issue:

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).

Code:


 Sub Temp  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    log.Message "PASS"  else    log.Message "FAIL"  end ifEnd SubFunction Function_A(Name)    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"  else    ReturnArray = Array("", "", False)    log.Message "Inside ELSE"  end if    Function1 = ReturnArray End Function

Originally posted by AlexKaras on September 07, 2011

Hi,

I’m afraid that my answer will not make you happy, but:

– It is near to impossible to handle runtime errors in VBScript;

– ‘On Error Resume Next’ is not recommended to be used by all good coding practices (not just for VBScript, but for VB as well);

– If you *really* need to handle possible runtime error, then you must remember, that every script code statement upon its execution resets the value of the Err built-in structure. This means that you *must* save the value of the Err.Number property *immediately* after the script line where you expect the runtime error, turn on error handling and only then check if there was an error or not. E.g.:

On Error Resume Next

OpenDBConnection()

iErr = Err

On Error GoTo 0

If (0 <> iErr) Then

…

Note, that if you expect runtime error to be possible in two subsequent script code lines then you must check the error value after each line. This is just because if the first line fails and second succeeds it will overwrite the error code set by the first line and you never know that the first line failed.

E.g.:

Correct:

On Error Resume Next

SubmitRequestHeader()

iErr = Err

If (0 <> iErr) Then

…

SubmitRequestBody()

iErr = Err

On Error GoTo 0

If (0 <> iErr) Then

…

Incorrect:

On Error Resume Next

SubmitRequestHeader()

SubmitRequestBody()

iErr = Err

On Error GoTo 0

If (0 <> iErr) Then

…

Hope that the above will help you somehow…

P.S. If you really need to use runtime script code error handling and did not invest too much to the code yet, my advice would be to consider DelphiScript or JScript both of which support the try/catch/finally runtime code errors/exceptions handling.

Originally posted by vthomeschoolmom on September 08, 2011

Alexei Karas [TeamAQA]

Said:

I’m afraid that my answer will not make you happy, but:

– It is near to impossible to handle runtime errors in VBScript;

– ‘On Error Resume Next’ is not recommended to be used by all good
coding practices (not just for VBScript, but for VB as well);

I cannot really agree with this statement. I DO agree that you don’t use On Error Resume Next in VB since you have something else to use Goto Line Label, and then VB will shoot the error up the call stack. But what is the alternative in VB Script? Just don’t handle errors? If you are doing file i/o or database writes, you would be foolhardy not to build in error handling.

As you say, check for error *and immediately On Error Goto 0* so you are not inadvertently eating any other errors. One of the more frustrating days in my life was an asp developer who basically put an ON ERROR RESUME NEXT at the top of his asp page. Then proceeded to do a bunch of File i/o operations (including writing to the ROOT of the web server duh) and database writes. He wondered why he was losing data. But could not understand what was wrong.

Originally posted by vthomeschoolmom on September 08, 2011

What kind of error have you put in the code? It seems to me that if the function returns a null, you can just test for isnull?