Course of Raku / Objects, I/O, and
exceptions / Exceptions / The CATCH phaser / Exercises / Handle and
continue
Solution: Handle and continue
Here is a possible solution to the task.
Code
for <ok bad ok> -> $item {
{
die 'boom' if $item eq 'bad';
say "processed $item";
CATCH {
default {
say "skipped ($item): {.message}";
}
}
}
}🦋 You can find the source code in the file handle-and-continue.raku.
Output
processed ok
skipped (bad): boom
processed okComments
The
CATCHphaser is inside the per-item block, so it handles adiefor just that one item. Because the exception is handled there, it never escapes to stop the whole loop.This is the typical use of
CATCHovertry: a failure in one iteration is dealt with locally, and the loop moves on to the next item. The middle item fails, but the twookitems are still processed.