[top] [prev] [next]

Try Except

A TRY-EXCEPT statement has the form:

    TRY
      Body
    EXCEPT 
      id_1 (v_1) => Handler_1
    | ... 
    | id_n (v_n) => Handler_n
    ELSE Handler_0 
    END
where Body and each Handler are statements, each id names an exception, and each v_i is an identifier. The "ELSE Handler_0" and each "(v_i)" are optional. It is a static error for an exception to be named more than once in the list of id's.

The statement executes Body. If the outcome is normal, the except clause is ignored. If Body raises any listed exception id_i, then Handler_i is executed. If Body raises any other exception and "ELSE Handler_0" is present, then it is executed. In either case, the outcome of the TRY statement is the outcome of the selected handler. If Body raises an unlisted exception and "ELSE Handler_0" is absent, then the outcome of the TRY statement is the exception raised by Body.

Each (v_i) declares a variable whose type is the argument type of the exception id_i and whose scope is Handler_i. When an exception id_i paired with an argument x is handled, v_i is initialized to x before Handler_i is executed. It is a static error to include (v_i) if exception id_i does not take an argument.

If (v_i) is absent, then id_i can be a list of exceptions separated by commas, as shorthand for a list in which the rest of the handler is repeated for each exception. That is:

    id_1, ..., id_n => Handler
is shorthand for:
    id_1 => Handler; ...; id_n => Handler

It is a checked runtime error to raise an exception outside the dynamic scope of a handler for that exception. A "TRY EXCEPT ELSE" counts as a handler for all exceptions.

[top] [prev] [next]