Forum Discussion

torus's avatar
torus
Contributor
2 years ago

javascript try/catch block

I thought try/catch blocks were supposed to help you avoid failures/exceptions. That is how they work in C# and from my understanding, this is how thye work in Javascript. Why is the below code still throwing an error? If it doesn't find the process called 'someProcess' the try/catch block should catch the error ... no error should exist in the test result. However, from the following code, I get a test failure stating that "The process 'someProcess' was not found".

 

function testing()
{
  try
  {
    var ml = Sys.Process("someProcess");
    //ml.Close();     
  }
  catch(e)
  {}
}

 

  • See Process Method - "The Process method returns a Process object by its application name and index. If the process is not found, the method returns an empty object and posts the message “The process … was not found” to the test log". Majority of the methods in TC are like this i.e. returns object or null.

     

    An empty object doesn't throw an exception.

3 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    See Process Method - "The Process method returns a Process object by its application name and index. If the process is not found, the method returns an empty object and posts the message “The process … was not found” to the test log". Majority of the methods in TC are like this i.e. returns object or null.

     

    An empty object doesn't throw an exception.

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Majority of the methods in TC are like this i.e. returns object or null.

      I would say that majority of methods in TC return either valid object if search succeeds/object exists or empty stub object with the only .Exists property available and set to False if search fails or object does not exist/was destroyed in the application.

      Null is almost never returned by 'native' TestComplete methods. Methods like .FindChildByXPath() may return null, but these methods are rather just a wrappers around XPath search engine than 'native' TestComplete methods.