Try statement

A try statement embeds a set of statements and makes it possible for your code to handle an exception that occurs when those statements run.

Background on exception handling

If a statement causes an exception and is embedded in a try statement, the result is as follows:
  1. Processing continues in one of the onException blocks that is within the try statement. Each block handles a particular kind of exception, and, at most, only one block runs in response to the exception that occurs.

    In most cases, the first block that matches the exception is the block that runs. However, AnyException is the most general kind of exception, and any block reserved for AnyException is the last to be considered.

    If an onException block runs, the exception is handled, and processing continues after the try block.

  2. If the exception does not match any of the onException blocks, the exception propagates. The term means that the exception is processed as if it had occurred in the function that invoked the function where the exception occurred:
    • If the function invocation was itself in a try statement, processing continues as described in step 1, but in the invoking function.
    • If the exception does not match any of the onException blocks in the invoking function, or if the function invocation was not in a try statement at all, the exception propagates further, to the function that invoked the higher-level function.
    Propagation continues until the exception is handled or until no other functions are in the function stack:
    • If the exception is handled, processing continues at the statement that follows the try statement that cleared the exception.
    • Alternatively, if no other functions are in the function stack, the run unit terminates.

If a statement causes an exception and is not embedded in a try statement, the exception propagates to the invoking function and potentially to yet higher-level functions, as suggested earlier.

If none of the statements in a try statement cause an exception or invoke a function from which an exception is propagated, none of the onException blocks in the try block run.

Use of a try block slows processing, so you might include only statements that are more likely than others to cause an exception. Examples include database-access statements and service invocations.

You can code a throw statement to explicitly cause an exception, which is then processed as any other exception is processed. For details, see “throw statement.”

Syntax



Syntax diagram for the try statement

exceptionRecordVariable
A declaration of a record with the Exception stereotype. The record includes the messageID and message field and possible additional fields as well, depending on the Record type.
statement
An EGL statement.

The following try statement embeds a statement that accesses a database, but could access several statements:

try
   add myPayment to ds;

   onException(ex SQLException)
     SysLib.writeStdOut(ex.message);

   onException(ex AnyException)
     SysLib.writeStdOut(ex.message);
end

The code responds to the exception by writing a message that is provided in the exception record. Other tasks might include rolling back a logical unit of work.

Compatibility

Table 1. Compatibility
Target Issue
Java No issues
JavaScript No issues.