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 seconds

Comments

  1. When the die throws, the rest of the block is abandoned — so the line $timeout = 60 never runs, and $timeout keeps its default of 30.

  2. The CATCH handles 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.

Course navigation

Report and recover   |   Quiz — CATCH