Handling exceptions

In case of emergence of any exceptional situation during script execution, TestComplete will stop the execution and display the error message. For example, when attempting to open a non-existing file, we will get an error message Unable to open the file.

To resolve this problem and continue script execution, we can apply a standard try…catch…finally construct.

Tip

This recipe contains information which is only applied for JScript programming language!

How to do it...

For example, let's suppose we need to read the contents of a file into a variable, if the file exists, after which the file should be deleted and a new eponymous empty file should be created.

  1. First of all, let's write up the code that is responsible for reading the contents of the original file and then deletes the same:
    var fileContent = "";
    var fileName = "c:\non-existing-file.txt";
    try
    {
      var f = aqFile.OpenTextFile(fileName, aqFile.faRead, aqFile.ctUTF8);
      fileContent = f.ReadAll();
      f.Close();
      aqFile.Delete(fileName);
    }

    This code has been placed into the try block at once, because it is fraught with an occurrence of an exceptional situation if the file does not exist. In this case, we need to simply ignore the exception by logging the occurrence to the log (in case, we have to analyze the flow of script execution later).

  2. Now, let's write up the contingency code for processing the exceptional situation.
    catch(ex)
    {
      switch(ex.number)
      {
        case -2147467259:
          Log.Message("File does not exist");
          break;
        default:
          throw ex;
      }
    }

    Note

    Prior to ignoring the exception, we check its code (ex.number) and proceed with ignoring the exception only in the case that its number matches with the code of the missing file error (that is, -2147467259). If, at this point, another exception takes place (for example, the file is existing, but it has been blocked by another application), we will generate the exception anew with the help of the throw instruction, since we do not know how to further treat the situation.

  3. And in the end, in the final block finally, we will execute actions that it is necessary to execute regardless of whether the exception has taken place or not:
    finally
    {
      aqFile.Create(fileName);
    }
  4. Here, we create a new file without second thoughts about the file with the same name that may already exist: in first case we delete the file ourselves, and in the second case the file was not in existence to begin with.

If we were to merge all the written code into a single function and launch it, the function would work successfully regardless of the fact whether the c:\non-existingfile.txt file exists or not.

How it works...

The try…catch…finally block is meant to process various nonstandard situations when we have a little knowledge of how the code will end up working:

  • In the try block, the code with potential errors is placed.
  • In the catch block, we handle these errors and continue to carry out all the necessary actions (for example, in our case, it is enough to enter the information on the arisen exception to the log). If we need to ignore all the errors, this block could be simply left empty, however, this is considered bad style in programming.
  • In the finally block, we are busy carrying out the actions which should be performed regardless of the emergent situation. This block is optional; however, there may be some situations where it is indispensable. For example, if we have opened a file and get some actions done over it, it has to be closed despite any possible errors that emerged, otherwise, this may tamper with the filesystem integrity.

There's more…

TestComplete does not signify the number of the emergent errors upon exceptional situation, which means that in order to find out the code of the error we are interested in, we first resort to the catch block to the following effect:

catch(ex)
{
  Log.Message(ex.number);
}

After this, the function has been called to make sure the file is missing on the disk. In the result, in the log we had the error's number, which was then used in the switch construct.

If, further on, we are up against other situations, which we would like to ignore or process in a different manner, we would simply add another block case and write all the necessary actions into it.

See also

  • The Handling exceptions from a different unit recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.252