Forum Discussion

aagapov's avatar
aagapov
Occasional Contributor
14 years ago

Impossible to throw an exception from the 'OnLogError' handler.

Hi,



Can anybody explain me what happens?

Why the code below does not work - TC just rises the error dialog "Exception thrown and not caught".

By the way it worked at least with TC v3. And this approach is very helpful with error handling.



function Main() {

    try {

        Log.Message("before call");

        Log.Error("aaa");

        Log.Message("after call");

    }

    catch (e) {

        Log.Message("catch - " + e.description);

    }

}



function GeneralEvents_OnLogError(Sender, LogParams)

{

    Log.Message("GeneralEvents_OnLogError");

    throw ("Exception");

}



Has anybody faced such kind of behaviour?

Are there any workarounds?



Thanks in advance for answers!

Cheers,

    Aleksey.

10 Replies

  • Hello Aleksey,



    The behavior you are facing is expected. Exceptions thrown within an event handler cannot be caught in the main script.



    We have a suggestion in our database to implement the ability to handle exceptions generated in event handlers, and your request has increased its rating.
    • karthikr1080's avatar
      karthikr1080
      New Contributor

      Hey Alexander,

       

      I am using TestComplete 10.60. Is this ability implemented in this version?

      I understand that the original author of this post this topic a while ago, but I still think having this ability is relevant today for bulding a custom test runner along the way described here.

       

      What alrernatives are available in 10.60 to do this approach?

       

      Thanks,

      karthik

      • karthikr1080's avatar
        karthikr1080
        New Contributor

        Ok, I tried this in TC 10.60. It is not working. The main script will not be able to catch an exception thrown by the OnLogError even handler routine. What alternatives are available to implement this in this version?

         

        Thanks,

        karthik

  • aagapov's avatar
    aagapov
    Occasional Contributor
    Hi Alex!



    That's cool; we will be waiting for it. But can you recommend any workaround?

    Thanks for your help.



    Regards,

       Aleksey
  • Hello Aleksey,



    Please describe your task in detail - we will try to help you with an appropriate suggestion.
  • aagapov's avatar
    aagapov
    Occasional Contributor
    Alex,



    well, the idea is fairly easy, there is a main script which manages other ones; it can launch them according the some input list file but need to have a possibility to interrupt the particular function execution in case of error (only one particular function!) and continue testing with the rest.

    Just for instance, Runner.Stop() function stops execution at all.



    For example we have main script like as follows:

    MainUnit.sj

    function Main() {

        try {

            while(/*read .lst file*/) {

                 try {

                     executeTestFunction("testUnit.test1");

                 }

                 catch (ex) {

                   /// do something

                 }

                 finally {

                    /// clean up

                 }

            }

        }

        catch (e) {

            Log.Message("catch - " + e.description);

        }

    }



    function EventControl1_OnLogError(Sender, LogParams)

    {

        Log.Message("Error desciption");

        throw "TSException";

    }



    Here is a generic test case:

    testUnit.sj



    test1()

    {

        try {

       .................

             Log.Error("Error happened");

       .................

        }

        catch (e) {

            throw "TestCaseException";

        }

    }



    I would like to note that such approach worked in the old version of TC at least in version 3.

    Thanks for your help.



    Regards,

       Aleksey.
  • Hello Aleksey,



    Runner.Stop has the CurrentTestOnly parameter that specifies whether the execution of a single test or the whole sequence of tests will stop (the second variant is the default value). Set this parameter to true to stop a single test:

    function EventControl1_OnLogError(Sender, LogParams)
     

    {


      ...
     

      Runner.Stop(true);
     

    }


    As an alternative, you can use the Test Items feature of TestComplete and set test items' Stop on error and Stop on exception options to Test Item. For more information, refer to the Stopping Tests on Errors and Exceptions help topic.
  • aagapov's avatar
    aagapov
    Occasional Contributor
    Alex,



    thank you for your response but approaches your mentioned cannot solve the idea I described:


    • because Runner.Stop(true); in my case stops execution at all including the main script too

    • the approach with test item suppose to make them manually but in my case tests (number and order) should be provided via the list file e.i. dynamically but I did not find the possibility to make the test item in runtime


    Regards,

       Aleksey
  • Hello Aleksey,



    There is no ideal solution to this task. Possible approaches can be:



    1. Instead of using the Main routine, call each routine specified in a file from an external .js script file (you can create it with Notepad). The contents are as follows: 

    var TestCompleteApp, IntegrationObject, FS, F, s;
     

    TestCompleteApp = WScript.CreateObject("TestComplete.TestCompleteApplication");


    IntegrationObject = TestCompleteApp.Integration;


    TestCompleteApp.Visible = true;


    IntegrationObject.OpenProjectSuite("C:\\ProjectSuite1\\ProjectSuite1.pjs");


    FS = WScript.CreateObject("Scripting.FileSystemObject");


    F = FS.OpenTextFile("C:\\RoutineList.txt", 1); //the RoutineList.txt file contains the list of the routines


    while(! F.AtEndOfStream){


      while (IntegrationObject.IsRunning())


      {}  


      s = F.ReadLine();


      IntegrationObject.RunRoutine("TestProject2", "testUnit", s);  


    }


    F.Close();


    Close TestComplete before executing this file.
     





    2. Manually add to and move test items in the Test Items collection of a project and control whether a test item needs to be run (enabled / disabled) or stopped on error from an external .js file. For more information, refer to the Automating TestComplete With the Integration Object section of the Working With TestComplete via COM - Overview help topic and the ProjectTestItem_COMAccess Object help topic.
  • aagapov's avatar
    aagapov
    Occasional Contributor
    Hi Alex,



    that is a good idea, thanks for advice!



    With best regards,

            Aleksey A.