19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.26 Try Expressions<br />

Syntax:<br />

Flow ::= try Expr + [catch Id (TraitType ⇒ Expr + ) + ] [forbid TraitTypes] [finally Expr + ] end<br />

A try expression starts with the special reserved word try followed by a sequence of expressions (the try block),<br />

followed by an optional catch clause, an optional forbid clause, an optional finally clause, and finally the<br />

special reserved word end . A catch clause consists of the special reserved word catch followed by an identifier,<br />

followed by a sequence of subclauses (each consisting of an exception type followed by the token ⇒ followed by<br />

a sequence of expressions). A forbid clause consists of the special reserved word forbid followed by a set of<br />

exception types. A finally clause consists of the special reserved word finally followed by a sequence of<br />

expressions. Note that the try block and the clauses form block expressions and have the various properties of block<br />

expressions (described in Section 13.11).<br />

<strong>The</strong> expressions in the try block are first evaluated in order until they have all completed normally, or until one of<br />

them completes abruptly. If the try block completes normally, the provisional value of the try expression is the<br />

value of the last expression in the try block. In this case, and in case of exiting to an enclosing label expression,<br />

the catch and forbid clauses are ignored.<br />

If an expression in the try block completes abruptly by throwing an exception, the exception value is bound to the<br />

identifier specified in the catch clause, and the type of the exception is matched against the subclauses of the catch<br />

clause in turn, exactly as in a typecase expression (Section 13.22). <strong>The</strong> right-hand-side sequence of expressions<br />

of the first matching subclause is evaluated. If it completes normally, its value is the provisional value of the try<br />

expression. If the catch clause completes abruptly, the try expression completes abruptly. If a thrown exception<br />

is not matched by the catch clause (or this clause is omitted), but it is a subtype of the exception type listed in a<br />

forbid clause, a new ForbiddenException is created with the thrown exception as its argument and thrown. <strong>The</strong><br />

exception thrown by the try block is chained to the ForbiddenException as described in Section 14.3.<br />

If an exception thrown from a try block is matched by both catch and forbid clauses, the exception is caught by<br />

the catch clause. If an exception thrown from a try block is not matched by any catch or forbid clause, the<br />

try expression completes abruptly.<br />

<strong>The</strong> finally clause is evaluated after completion of the try block and any catch or forbid clause. <strong>The</strong> expressions<br />

in the finally clause are evaluated in order until they have all completed normally, or until one of them<br />

completes abruptly. In the latter case, the try expression completes abruptly exactly as the subexpression in the<br />

finally clause does.<br />

If the finally clause completes normally, and the try block or the catch clause completes normally, then the<br />

try expression completes normally with the provisional value of the try expression. Otherwise, the try expression<br />

completes abruptly as specified above.<br />

For example, the following try expression:<br />

try<br />

inp = read(file)<br />

write(inp,newFile)<br />

forbid IOException<br />

end<br />

is equivalent to:<br />

try<br />

inp = read(file)<br />

write(inp,newFile)<br />

catch e<br />

109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!