Course of Raku / Objects, I/O, and
exceptions / Exceptions / The CATCH phaser / Exercises / Report and
recover
Solution: Report and recover
Here is a possible solution to the task.
Code
my $timeout = 30;
{
die 'config missing';
$timeout = 60;
CATCH {
default {
say "warning: {.message}; keeping default";
}
}
}
say "timeout is $timeout seconds";🦋 You can find the source code in the file report-and-recover.raku.
Output
warning: config missing; keeping default
timeout is 30 secondsComments
When the
diethrows, the rest of the block is abandoned — so the line$timeout = 60never runs, and$timeoutkeeps its default of30.The
CATCHhandles the exception (printing a warning), so the program does not stop. It carries on with the default value, which is exactly what recovering from an error means: note the problem, fall back to something safe, and continue.