Phantom Documentation

[Help Home] [Phantom Home]
exception

The exception data type is used for exception handling. Exceptions are errors, warnings, and failures, and can also be benign 'OK' messages and user defined exceptions. Whenever an internal error, warning, or failure occurs, an exception is generated. The exception can be processed using the try-catch statements.

An exception has two parts: the exception state and a message. The exception state is an integer representing the following:


0 = Error
1 = OK
2 = Warning
3 = Not used
4 = Failure


The exception state can also be a user defined value set using the Set member function.

The message is any string containing a message about the exception.

The exception data type can also be thrown manually using the throw method. This is useful for user-defined errors, warnings, and failures.

All exceptions except the OK type can be caught and processed using try-catch statements.

The exception data type supports the following member functions:

GetMessage
GetState
Set
SetError
SetFailure
SetOK
SetWarning
throw


Example Code

# Declare some exceptions
exception err;
exception warn;
exception fail;
exception OK;
exception user;

# Define the exceptions
err.SetError("Sample Error");
warn.SetWarning("Sample Warning");
fail.SetFailure("Sample Failure");
OK.SetOK("Sample OK");
user.Set(10, "Sample User Message");

# Get a message
disp(OK.GetMessage());

# Get a state
disp(OK.GetState());

# Throw some exceptions
# Note that OK will do nothing
warn.throw();
OK.throw();
user.throw();

# The Failure and Error exceptions
# must be caught, or else the script
# will exit.
try{
  fail.throw();
}catch(exception e){
  disp("Failure caught: " + e.GetMessage());
  disp("State: " + string(e.GetState()));
}

try{
  err.throw();
}catch(exception e){
  disp("Error caught: " + e.GetMessage());
  disp("State: " + string(e.GetState()));
}

# Catch a user-defined exception
try{
  user.throw();
}catch(exception e){
  disp("User defined caught: " + e.GetMessage());
  disp("State: " + string(e.GetState()));
}



Output
Sample OK
1
** WARNING: (1) from 'Sample.psc' on line 25 - Sample Warning
Custom message from 'Sample.psc' on line 27 - Sample User Message
Failure caught: Sample Failure
State: 4
Error caught: Sample Error
State: 0
User defined caught: Sample User Message
State: 10


See Also:
Variables, try-catch


Copyright 2000-2011 Phantom Automated Solutions