% % Grail % -module(grail). -author('Alan'). -define(else, true). % % -- Public -- % -export([throws/1, errors/1, exits/1]). -export([sword/1, knight/1]). throws(F) -> try F() of _ -> ok catch ThrowMsg -> {throw, caught, ThrowMsg} end. errors(F) -> try F() of _ -> ok catch error:ErrorMsg -> {error, caught, ErrorMsg} end. exits(F) -> try F() of _ -> ok catch exit:ExitMsg -> {exit, caught, ExitMsg} end. sword(1) -> throw(slice); sword(2) -> erlang:error(cut_arm); sword(3) -> exit(cut_leg); sword(4) -> throw(buttercup); sword(5) -> erlang:error(inconceivable); sword(6) -> exit(storm_the_castle); sword(_) -> unknown_sword_param. knight(Attack) when is_function(Attack, 0) -> try Attack() of _ -> "None shall pass!" catch throw:slice -> "It is but a scratch"; error:cut_arm -> "I've had worse."; exit:cut_leg -> "Just a flesh wound"; _:_ -> wrong_movie end. % % -- Private -- %