Course of Raku / Objects, I/O, and
exceptions / Exceptions / Catching exceptions with try / Exercises / Try or default
Solution: Try or default
Here is a possible solution to the task.
Code
my $r = try { die 'no' } // 'default';
say $r;🦋 You can find the source code in the file try-or-default.raku.
Output
defaultComments
The failing
tryblock evaluates to an undefined value.The defined-or operator
//therefore returns its right-hand side,'default', giving a clean way to recover with a fallback in a single line.
Course navigation
← Try or default
| Quiz — try →