10.2 Exceptions
expression | ||||||||||||||
| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
|
Returns the value(s) of the body sequence, but runs the body or expression of an ~initially clause when entering the try body (whether normally or by a continuation jump) and the body or expression of a ~finally clause when leaving the try body (whether normally or by a continuation jump, including exception throws).
If an exception is thrown during the the body sequence, the control escapes to the context of the try body sequence (i.e., “inside” the ~initially and ~finally guards) and the ~catch cases are tried in order. When a ~catch binding matches, then the result of the try form is the body of the ~catch clause. If no ~catch clause matches, the exception is re-thrown. Breaks are disabled while attempting to match a ~catch clause or evaluating its body.
> try:
1/0
~catch exn :: Exn.Fail.Contract.DivideByZero:
"handled"
"handled"
> try:
~initially: println("in")
1/0
~catch _:
println("ignoring all exceptions!")
0
~finally: println("out")
in
ignoring all exceptions!
out
0
> def k:
try:
~initially: println("in")
Continuation.capture k: k
~finally: println("out")
in
out
> k(block:
println("pre")
"again")
pre
in
out
"again"
> Continuation.in k:
println("body")
"again"
in
body
out
"again"
If ~escape_as escape_id is before the block after try, then escape_id is bound for use in the body of the try form as an escape continuation function that jumps out of the try form. The arguments provided to the function are returned as the results of the try form. Calling escape_id is an error when outside the dynamic extent of evaluating the try form.
> try ~escape_as escape:
println("doesn't get here")
0
The last body form of try is not in tail position if any of ~escape_as, ~initially, ~catch, or ~finally is present. If none are present, the try form is the same as block.
class | ||
| ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||
| ||
class | ||