next up previous index
Next: Collections Up: Control structures Previous: Looping and closures   Index

Exception handling

A couple of methods are defined on closures to specialize error and exception handling.

In error.cecil:

method handle_system_errors(cl:&():void):bool;
The handle_system_errors method executes its argument closure. Any Cecil errors (e.g., error or exit calls) caused during evaluation of the closure are caught and suppressed. The boolean return value indicates whether an error occurred.

method unwind_protect(cl:&():`T, on_return:&():void):T;

The unwind_protect method allows a ``clean up'' closure to be executed whenever control returns through the unwind_protect method call, either normally, via a non-local return, or via a Cecil error. unwind_protect first invokes its cl closure argument. When control returns from this invocation, the on_return closure is invoked. If the on_return closure returns normally, the returning of the cl closure is resumed: if the cl closure returned normally, the result of this closure is returned as the result of the unwind_protect method; otherwise it continues to throw whatever exception the body did. If the on_return closure returns abnormally, either via a non-local return or a Cecil runtime error, then this result supercedes the original suspended result of the cl closure. (unwind_protect is similar to the like-named construct in Common Lisp.)

method on_error(cl:&():`T, on_error:&():void):T;

The on_error method allows a ``clean up'' closure to be executed whenever control returns through the on_error method call abnormally, either via a non-local return or via a Cecil error. on_error first invokes its cl closure argument. If control returns normally from cl, then on_error returns result of this closure. If, on the other hand, control returns from cl either via a non-local return or a Cecil runtime error, the on_error closure is invoked. If this closure completes normally, the abnormal returning of the cl closure is resumed (either continuing the non-local return or the Cecil run-time error raised by cl). If the on_error closure returns abnormally, either via a non-local return or a Cecil runtime error, then this result supercedes the original suspended abnormal result of the cl closure. on_error is like unwind_protect, except that the on_error block is only run if the cl block has an abnormal result.


next up previous index
Next: Collections Up: Control structures Previous: Looping and closures   Index

Cecil/Vortex Project