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.
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.
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).
catch(ex) { switch(ex.number) { case -2147467259: Log.Message("File does not exist"); break; default: throw ex; } }
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.
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); }
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.
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:
try
block, the code with potential errors is placed.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.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.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.