Forum Discussion

mst1's avatar
mst1
Occasional Contributor
12 years ago

Vbscript Err.Number in different files

hello,



I am currently busy with building a TestComplete project using VBScript. The script code is currenlty splitted over different files. I used the 'USEUNIT statement to be able to  call functions from another file.



I'm actually experiencing problems with the "global" Err object from vbscript.

The case is as next:



In the first file I have some functionallity and in some cases I am raising an error myself.

After that I call a general (custom) function to check error numbers. This function is defined in the other files. So for example:



FileA:

'USEUNIT FileB

Sub MyFunction

 

  on error resume next          'start local error catching

 

  'raise a custom error

  err.raise vbObjectError + 1, "MyFunction", "Customer error"

  ErrorChecker               'check for errors and handle them  

  on error goto 0             'stop local error catching



End Sub





The errorchecker function is defined in FileB:

Sub ErrorChecker

  If Err.Number <> 0 Then

    'handle the error

    Err.Clear

  End If    

End Sub





However, the raised error is not handled. Step-by-step debugging showed me that:

In fileA the error is raised properly

As soon as the execution reached FileB, the err.number is 0 (and the error is not handled therefore)

As soon as the execution is returned to FileA again, the error is exists (again)



So as far as I can see, the Err object is NOT global in TestComplete, but local per scriptfile.



Does someone know if that's true, and if there is a workaround to make the Err object really global?



Thanks for your reply in advance!



3 Replies

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Frank, you are correct, Exception handling between Script Units is not supported and as far as I am aware, there is no way around this.


     


    In your case, you could change ErrorChecker to accept a reference parameter and pass the local Err object from FileA to it, e.g.


     


    'USEUNIT FileB


    Sub MyFunction


      on error resume next          'start local error catching


      'raise a custom error


      err.raise vbObjectError + 1, "MyFunction", "Customer error"


      Log.Message( Err.Number) ' Logs -2147221503


      ErrorChecker Err           'check for errors and handle them


      Log.Message( Err.Number) ' Logs 0


      on error goto 0             'stop local error catching


    End Sub


     


    Sub ErrorChecker( ByRef errObj )


      If errObj.Number <> 0 Then


        'handle the error


        errObj.Clear


      End If    


    End Sub


     


    Hope this helps,


    Phil

  • mst1's avatar
    mst1
    Occasional Contributor
    Hello Phil,



    Thanks for the reply.

    That is a solution, but it give me another problem.

    In my previous example, I raised the error in the same file as where I did the ErrorChecker call, so the reference to the Err object is the reference to the original Err object.



    But in fact, my error is raised in another file as well.

    In FileA, I call a function, located in FileC. There is an error raised in FileC, and this error will also be raised in FileA, because in FileA whas the function caller located.



    Now in File A I will call my error checker (which is located in FileB) with the reference to the "local" error object. This is the error object of FileA.



    The error checker will handle the Error perfect, and will clear the Error from FileA as well.

    But the very first Err source (raised in FileC) is still not cleared.



    The example code of File A:



    On Error Resume Next

    MyFunction 'Calling function located in FileC

    'Error raised in FileC

    'Because "I" am The caller, The error is raised in THIS file as well (FileA)



    ErrorChecker Err  'call the errorchecker with reference to the error object of THIS file (fileA)

    On Error GoTo 0

    'Now the error of THIS file is cleared, but the error in FileC still exsist



  • mst1's avatar
    mst1
    Occasional Contributor
    Just for information, I made the next workaroud.

    I created a function which give's a reference to the Err object of each unit file.



    This way, I can easily catch all the error, from each file (and clear them in each file as well).

    The bad thing is that I really should not forget to add this when I add another unit file in the future, but the positive thing is that I just can raise and catch errors locally the normal way (with the "local" err object)