Course of Raku / Objects, I/O, and exceptions / Exceptions / Catching exceptions with try / Exercises / When nothing goes wrong

Solution: When nothing goes wrong

Here is a possible solution to the task.

Code

my $r = try { 21 * 2 };

say $r.defined;
say $r;

🦋 You can find the source code in the file try-succeeds.raku.

Output

True
42

Comments

  1. When the try block runs without an exception, it simply evaluates to the value of the block.

  2. So $r holds 42, and $r.defined is True.

Course navigation

When nothing goes wrong   |   Try or default