Language Reference/Terms/Try-catch-finally

The try-catch-finally statement provides means for dealing with exceptions that may occur in a given block of code.

: try  -list end try

: one of   catch  do - finally -

: 

A try-construction thus have a Term and a list of catch and finally handlers.

The  and the handlers are not allowed to leave backtrack points (i.e. they cannot have mode multi or nondeterm).

A try-construction with more than one handler is equivalent to nesting several try-constructions with one handler each. I.e. the following term:

try <Body> catch <Var1> do  <Handler1> finally <Handler2> catch <Var2> do   <Handler3> end try

Is equivalent to these nested terms:

try try try <Body> catch <Var1> do           <Handler1> end try finally <Handler2> end try catch <Var2> do   <Handler3> end try

try-catch
Consider the try-catch construction

try <Body> catch <Var> do   <Handler> end try

First <Body> is evaluated.

If <Body> fails or succeeds the whole try construction fails or succeeds, respectively. I.e. if <Body> does not terminate with an exception the try construction corresponds to evaluating <Body>.

If <Body> terminates with an exception, then the exception is caught by the catch handler. Meaning that first <Var> is bound to the exception (in PFC context the exception will be a traceId</vp>) and then <Handler> is evaluated. In this case the construction behaves as if <Handler> is evaluated with <Var> bound to the caught exception.

Notice, that no bindings are made by <Body> if it terminates with an exception.

try-finally
Consider the try-finally construction:

try <Body> finally <Handler> end try

The purpose of the construction is to evaluate the <Handler> after the <Body> no matter how the <Body> terminates, i.e. whether it succeeds, fails or terminates with an exception (it cannot leave backtrack points).

The evaluation is like this

First <Body> is evaluated.


 * If <Body> succeeds: <Handler> is executed and the try-finally construction succeeds.
 * If <Body> fails: <Handler> is executed and the try-finally construction fails.
 * If <Body> terminates with an exception <Exn> : <Handler> is executed and the try-finally construction terminates with the exception <Exn>.