Phantom Documentation

[Help Home] [Phantom Home]
try-catch

Syntax:

try
{
  Statement1;
  Statement2;
  ....
}
catch(exception e)
{
  Statement3;
  Statement4;
  ....
}

Where,

exception e- A variable containing the caught exception

Description:
A try-catch statement can be used to catch and handle exceptions thrown either internally by the interpreter or by a script using the exception data type. Caught exceptions are not processed normally by the interpreter, but can be analyzed using the statements after the catch statement.

A try-catch statement works by processing all of the script statements in the try section. If an exception is thrown, execution immediately moves to the catch section. The exception will be placed in the exception variable declared by the catch statement. The exception state and message can then be analyzed using the exception member functions.

Based on the analysis, the exception can be ignored completely, re-thrown and processed normally by the interpreter, or a new exception can be generated.

A catch statement will catch all exception types except for the OK type. For a list of types, see the exception data type.

Example Code

# Declare and define an error
exception err;
err.SetError("Sample Error");

# Declare and define a warning
exception warn;
warn.SetWarning("Sample Warning");

# Catch a thrown error
try{
  err.throw();
}catch(exception e){
  disp("Exception Caught: " + e.GetMessage());
  disp(" State = " + string(e.GetState()));
}

# Catch a thrown warning
try{
  warn.throw();
}catch(exception e){
  disp("Exception Caught: " + e.GetMessage());
  disp(" State = " + string(e.GetState()));
}

# Do not catch, and see that the script
# exits when the error is thrown

warn.throw();
err.throw();

disp("Will not get here!");

Output
Exception Caught: Sample Error
State = 0
Exception Caught: Sample Warning
State = 2
** WARNING: (1) from 'Sample.psc' on line 30 - Sample Warning
** ERROR: (1) from 'Sample.psc' on line 31 - Sample Error


Failure and error exceptions generated within a user function can be processed either with a try-catch in the user function or in a try-catch outside the user function. Any failure or error exception not processed by a try-catch in a user function will be passed back to the calling function or script. However, the message will be lost and replaced with a message indicating that an exception occurred in a function, and the original exception will still be displayed on the screen and the error counter incremented (if it is an error). Therefore, if an exception message is important to catch processing, the message should be caught in the user function where it is generated.

Example Code

# Sample user function
function void user_function(int i){
  exception ex;

  # Error out if i > 10
  if(i > 10){
    ex.SetError("i must be < 10");
    ex.throw();
  }

  try{
    # Warn and process if i >= 8
    if(i >= 8){
      ex.SetWarning("i should be less than 8");
      ex.throw();
    }
  }catch(exception e){
    disp(e.GetMessage());
    i = 7;

    # re-throw the message
    e.throw();
  }

  disp("i = " + string(i));

  # User defined exception
  ex.Set(6, "i is OK");
  ex.throw();
}

# Catch error exception in user_function. If
# this exception is not caught, execution will
# end.
try{
  user_function(12);
}catch (exception e){
  disp("Exception Caught: " + e.GetMessage());
  disp(" State = " + string(e.GetState()));
}

# Warning will not be caught, nor will
# user exception
try{
  user_function(9);
  user_function(6);
}catch (exception e){
  disp("Exception Caught: " + e.GetMessage());
  disp(" State = " + string(e.GetState()));
}



The output from the above example is as follows

Output
** ERROR: (1) from 'user_function' on line 9 - i must be < 10
Exception Caught: Error in function 'user_function'. (1125)
State = 0
i should be less than 8
** WARNING: (1) from 'user_function' on line 23 - i should be less than 8
i = 7
Custom message from 'user_function' on line 30 - i is OK
i = 6
Custom message from 'user_function' on line 30 - i is OK


Note that any variables created in the catch statement are local only to the catch statement. Variables in the try statement are available outside the try.

The following is an example catching internal Phantom errors:

Example Code

# Include Phantom Target Declarations
use "PhantomTarget.dec";
System("PhantomTarget.exe");

try{
  # This will 'accidentally' close
  # PhantomTarget
  PhantomTarget.TypeKeys("<ALT-F4>");

  # Will still try to perform actions
  PhantomTarget._Controls._RadioButton.Click();
}catch(exception e){
  # PhantomTarget is no longer found
  disp("Exception caught: " + e.GetMessage());

  # Restart PhantomTarget, continue
  System("PhantomTarget.exe");
}

try{
  # Some typo will cause an error
  PhantomTargt.Maximize();
}catch(exception e){

  # Clean up the script by catching the
  # error and closing PhantomTarget
  PhantomTarget.Close();

  # Rethrow the error so it will be displayed
  # in the output.
  e.throw();
}



Output
Exception caught: Window '' not found (Class=Afx:, Tag=Phantom Target). (P00100)
** ERROR: (1) from 'Sample.psc' on line 33 - Variable 'PhantomTargt' not found. (3324)


See exception for additional examples.

See Also:
exception


Copyright 2000-2011 Phantom Automated Solutions