Handling Exceptions in Scripts

Applies to TestComplete 14.10, last modified on June 5, 2019

During the script run different exceptions may occur. These can be exceptions that occur due to errors in script code and exceptions that occur in the application under test. First, we will explain general technique of exception handling in scripts and then continue with handling exceptions that occur in tested applications.

TestComplete does not provide any new functions or statements that handle exceptions in scripts. You can use the statements provided by the scripting languages.

JavaScript, JScript, C#Script and C++Script

To handle exceptions in JavaScript and JScript use the try{}…catch{}…finally{} statement. catch holds code that is executed when an exception occurs within the try block. finally contains instructions which are executed always, regardless of whether or not exceptions occur in the try block. For more information see the description of the try...catch statement in the JavaScript documentation or the description of the try...catch...finally statement in the JScript documentation. The following code demonstrates how you can handle exceptions in JavaScript routines:

JavaScript

try
{
  // Do something
  ...
}
catch (e)
{
 // Posts an exception message to the test log
  Log.Error(e.message);
}
finally
{
  ...
}

JScript

try
{
  // Do something
  ...
}
catch (e)
{
 // Posts an exception message to the test log
  Log.Error(e.description);
}
finally
{
  ...
}

Since C#Script and C++Script are based on JScript, you can use the same try... catch... finally statement. For example:

C++Script, C#Script

try {
  // Do something
  ...
}
catch (e) {
  // Posts an exception message to the test log
  Log["Error"](e["description"]);
}
finally
{
  ...
}

Although TestComplete supports cross-unit exception handling, there are some limitations:

Python

To handle exceptions in Python, use the try…except…else…finally statement.

The except block is executed when an exception occurs within the try block.

The optional else block is executed only if there were no exceptions after try and before finally.

The finally block contains instructions that are always executed at the end, regardless of whether exceptions occurred in the try block.

For more information, see the description of https://docs.python.org/3.6/tutorial/errors.html exceptions in the Python documentation. The code below demonstrates how you can handle exceptions in Python routines:

Python

def test():
  try:
    # Do something
    ...
  except Exception as e:    
    Log.Error(str(e))
  else:
    # Do something else if there were no errors
    # You can handle other potential exceptions here:
    try:
      ...
    except Exception as e:    
      Log.Error(str(e))
    ...
  finally:
    # Finalize the run
    ...

VBScript

To handle exceptions, you can use the Err object and the On Error statement. To learn more about them, refer to the description of the Err object and of the On Error statement in the VBScript documentation in the MSDN Library. The following code demonstrates how you can handle exceptions in VBScript routines:

VBScript

Err.Clear ' Resets the exception indicator
On Error Resume Next ' If an exception occurs,
                    ' TestComplete will continue running
                    ' the script rather than stop it
  ' Do something
  ...
If Err.Number <> 0 Then
  ' An exception occurred !!!
  ' Posts the exception messages to the test log
  Log.Error Err.Description
Else
  ' No exceptions
  ...
End If

To cancel the effect of the On Error Resume Next statement, call On Error GoTo 0.

VBScript

Sub Main
  Err.Clear
 On Error Resume Next
 
  i = j / 0 ' Exception occurs
  Log.Message Err.Description ' Posts the exception description to the log. 
                             ' The exception is handled.
 
On Error GoTo 0
 
  i = j / 0 ' Exception occurs
            ' This exception is not handled
End Sub

DelphiScript

Methods that handle exceptions in DelphiScript are similar to those that handle exceptions in Delphi. This includes the try... except... end and try... finally... end statements. These statements handle exceptions that occur within the try... except and try... finally blocks.

The except... end block is executed only if an exception occurs. Use it to perform specific actions as a response to an exception. If no exceptions occur, the control is passed to the instruction next to the end statement.

The finally... end block is always executed. Use it to ensure that certain operations in your code are completed whether or not exceptions occur.

To obtain the exception message in scripts, use the ExceptionMessage function. The following code illustrates exception handling in DelphiScript routines:

DelphiScript

try
 // Do something
  ...
except
  // An exception occurred !!!
  // Posts the exception message to the test log
  Log.Error(ExceptionMessage);
end;

If you have recursive code in the try block, after TestComplete exits the recursive loop, it will run the code in the except block regardless of whether any errors have occurred in the try block or not.

Exceptions That Occur in the Application Under Test

TestComplete can handle exceptions that occur in the application under test, but this is only possible if the tested application is an Open Application. To handle these exceptions use the same statements that you use to handle exceptions in your scripts (since calls to Open Application’s methods do not differ from calls to any other script functions). These statements are language-specific and they were described in the previous sections of this topic. However, there are some specifics:

  • TestComplete handles exceptions only when the script calls methods or properties of an application object. This is not possible with ordinary “closed” applications. Suppose that an exception occurs after pressing a button on a form. TestComplete will handle this exception if the script simulates the keystroke using the following instruction:

    w.VCLObject('Button1').NativeDelphiObject.Click;

    TestComplete will not handle the exception, if you use the following code to simulate the button pressing:

    w.VCLObject('Button1').Click(); // Exception is not handled !!!

  • If an exception occurs during a method call made through the Debug Info Agent™ (the methods called through the Agent are displayed under the Debug Agent node in the Object Browser), the scripting engine’s behavior depends on the Advanced exception handling property of your TestComplete project. If this property is enabled, then the scripting engine receives notification about the exception and the exception-handling statements (try/catch, try/except, etc.) come into play. If the property is disabled (default), the exception is hidden and the method returns a null value (0, null reference, empty string and so on). This mode is used for compatibility with earlier TestComplete versions. You can enable the project’s Advanced exception handling property if you do not run legacy projects. You can also control the property’s state from scripts by using the DebugAgent.AdvancedExceptionHandling property.

An exception that occurs in the application under test usually acts as an unexpected window in TestComplete (this exception message box is unexpected,  because it did not appear when the script was being recorded). Using project settings of the Playback group and the OnUnexpectedWindow event handler, you can handle unexpected windows in the desired way. For more information, see Handling Unexpected Windows. This way applies to both “Open” and “black-box” applications.

To view the call stack for exceptions, you can use the DbgServices object. If a tested application is launched by one of the methods of this object, TestComplete will trace errors and exceptions in this application and if an exception occurs, TestComplete will show the call stack for it in the test log. See Tracing Events and Exceptions With Debug Services. This approach also works for both “Open” and “black-box” applications.

See Also

Script Tests
Handling Unexpected Windows
Tracing Events and Exceptions With Debug Services

Highlight search results