Course of Raku / Objects, I/O, and exceptions / Exceptions / Soft failures / Exercises / A fallback value
Solution: A fallback value
Here is a possible solution to the task.
Code
sub get($x) {
fail 'bad' if $x == 0;
return $x;
}
say get(5) // 'default';
say get(0) // 'default';🦋 You can find the source code in the file fallback-value.raku.
Output
5
defaultComments
get(5)returns5normally, so//keeps it.get(0)returns an undefinedFailure, so//falls back to'default'. Because aFailureis undefined,//handles it quietly, without throwing.
Course navigation
← A fallback
value | Quiz —
fail →