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

default

Comments

  1. The failing try block evaluates to an undefined value.

  2. 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