defer.j
use io;
func undo(step as string) {
io.printf("undo %s\n", $step);
}
func setup(fail as bool) {
defer io.printf("setup done\n");
io.printf("open\n");
errdefer undo("open");
io.printf("reserve\n");
errdefer undo("reserve");
if ($fail) { throw Error{kind: "value", message: "no config"}; }
return "ready";
}
def r as string init setup(false);
io.printf("returned %s\n", $r);
io.printf("--\n");
try {
setup(true);
} catch (e) {
io.printf("caught %s: %s\n", $e.kind, $e.message);
}