7. Failures
A failure is something a procedure can give back instead of its result, and every call to one that can fail must deal with it, as in Unio (D22, Unio D23). Unio's standard, chapter 8, has the rules in full.
guess = be.Whole(typed) ?? 0
save(game) ?? ignore
load_level(name) ?? crash
best = load_best() otherwise
case not_found
instead 0
end
bag.add("Gem") otherwise
case full
show_msg("Your bag is full!")
end
?passes the failure up (Unio D23).??handles it on one line, with a value,ignore,crashor a handler (Unio D23, D121, D98).- What deals with a failure runs only when there is one: a fallback after
??, as?? default_level(), and a handler's arguments are worked out only when the call fails (D130). otherwisehandles each failure in its owncase, ending ininstead,return,breakor failing on purpose, or simply ending when the call gives back nothing (Unio D40, D186).insteadgives a value in place of the failed call's result, and the line carries on;returnleaves the procedure (D157).insteadis the last line of its case: a line after it can never run, so it's refused, as one afterreturnorbreakis (D80, Unio D57).
In a script, ?? crash stops the script, not the host, which is told (D14, chapter 10).
otherwise and case lines have no : (D9), and the otherwise closes with one end (D20).
Each case runs until the next case, as in Unio (D37).
Failing on purpose
fail name fails on purpose (D44). A procedure that can fail lists its failures on its first line, as ! [not_found, broken], after -> Type if it has one: every fail in its body, and every failure it passes up with ?. A list
that doesn't match the body is refused, with the right list as the fix (D44, D45, D84). A door can fail too, with
failures its host declares (D44).
proc load_level[name: Text] -> Level ! [not_found, broken]
if not level_exists(name)
fail not_found
else
return read_level(name)?
end
end
Handlers
A handler is a named, reusable set of cases. Handlers live in a handler group, a named set reached with ::, as
Cyborg reaches every named set (D110, Cyborg D488, Unio D41 to D44):
group.handler level_failed
handles [load_level, load_save]
_::start_over
case not_found
instead 1
case broken
instead 1
end
_::start_at[level: Whole]
case not_found
instead level
case broken
instead level
end
end
a = load_level("forest") ?? _::start_over
b = load_save("slot1") ?? level_failed::start_at(3)
c = load_level("cave") otherwise
include _::start_over
end
group.handler namedeclares a group: "a handler kind of group". It isn't a family, since its members aren't kinds of its name (D110).- Each member is declared as
_::name,_being the group it's inside (D110, D179). handles [ … ], an optional first line, lists the procedures the group is for: any that can fail, the script's own, narrowed ones asspawn.boss, the host's doors, and built-ins asbe.Whole(D129). Right after a call to one of them,_::start_overreaches into the groups that handle it,_being the group assumed from the call (D179); if two such groups both have that member, the short form is refused there (D110).- The full form,
level_failed::start_over, works anywhere (D110). A::with nothing on its left is refused (D179). - On one line, a handler follows
??, as a value,ignoreorcrashwould;otherwiseis only ever a block (D98). - It's used as if its cases were written at the call, so it must cover every failure still possible there (Unio
D40, D42). Its cases for failures that call can't give are allowed and never run there, since a handler is shared
across calls; a case written straight into an
otherwiseblock for a failure its call can't give is refused (D131). - A handler that can
returnorbreakis marked^where it's used, as?? _::^give_upor?? level_failed::^give_up; the mark is required on one that can jump and refused on one that can't (Unio D42, D182). Afailinside a handler's case leaves the procedure too, so it counts as a jump (D129). includeinside anotherwiseblock brings in a handler's cases beside the block's own; together they cover every failure, none twice (Unio D43).- A handler can take parameters, written as a procedure's are, and is given arguments where it's used (Unio D44). The arguments are worked out only when the call fails, right before the handler's cases run (D130).