Course of Raku / Objects, I/O, and
exceptions / Exceptions / Catching exceptions with try / Exercises / Catch the error
Solution: Catch the error
Here is a possible solution to the task.
Code
my $ok = try { 6 * 7 };
say $ok;
my $bad = try { die 'broken' };
say $bad.defined;
say $!.message;🦋 You can find the source code in the file catch-the-error.raku.
Output
42
False
brokenComments
When the block runs without error,
trysimply evaluates to the block’s value, so$okholds42.The
dieinside the second block throws an exception, so that block evaluates to an undefined value and$bad.definedisFalse.The caught exception is stored in the special variable
$!, and$!.messagereturns the text it was given,broken.