Siebel eScript Language Reference > Siebel eScript Language Overview > Siebel eScript Statements >

try Statement


The try statement is used to process exceptions that occur during script execution.

Syntax

try
{
   statement_block
}
catch
{
   exception_handling_block
   [throw exception]
}
finally
{
   statement_block_2
}

Placeholder
Description

statement_block

A block of code that may generate an error

exception_handling_block

A block of code to process the error

exception

An error of a named type

statement_block_2

A block of code that is always executed, unless that block transfers control to elsewhere in the script

Usage

The try statement is used to handle functions that may raise exceptions, which are error conditions that cause the script to branch to a different routine. A try statement generally includes a catch clause or a finally clause, and may include both. The catch clause is used to handle the exception. To raise an exception, use the throw statement (see throw Statement).

When you want to trap potential errors generated by a block of code, place that code in a try statement, and follow the try statement with a catch statement. The catch statement is used to process the exceptions that may occur in the manner you specify in the exception_handling_block.

The following example demonstrates the general form of the try statement with the catch clause. In this example, the script continues executing after the error message is displayed:

try
{
   do_something;
}
catch( e )
{
   TheApplication().RaiseErrorText(Clib.rsprintf(
      "Something bad happened: %s\n",e.toString()));
}

The finally clause is used for code that should always be executed before exiting the try statement, regardless of whether the catch clause halts the execution of the script. Statements in the finally clause are skipped only if the finally clause redirects the flow of control to another part of the script. The finally statement can be exited by a goto, throw, or return statement.

Here is an example:

try
{
   return 10;
}
finally
{
   goto no_way;
}

no_way: statement_block

Execution continues with the code after the label, so the return statement is ignored.

You can use the try statement to process the exceptions thrown by CORBA objects, and to access their data members and exception names. If the exception contains nested objects or CORBA structures, they are skipped. For more information on creating and using CORBA objects in eScript, read CORBACreateObject() Method.

Example

The following example processes a CORBA exception. It assumes that the user is running the Account server and calling the function clear_balance(-1) on it. This raises the exception AccountFrozen, which is described in the CORBA IDL file as follows:

exception AccountFrozen {
   float mmx;
   long minimum;
};

This server assigns the value 7777.5555 to the variable mmx, and assigns 50 to the variable minimum, and then throws the AccountFrozen exception object. The eScript code might resemble the following:

try
{
   var cObj = CORBACreateObject("Account");
   var d1 = cObj.clear_balance(-1);
}
catch(obj)
{
   TheApplication().RaiseErrorText(obj.name + "\n" + obj.mmx + "\n" + obj.minimum);
}

See Also

throw Statement

Siebel eScript Language Reference