Course of Raku / Objects, I/O, and exceptions / Exceptions / Custom exceptions / Exercises / Two exception types
Solution: Two exception types
Here is a possible solution to the task.
Code
class TooSmall is Exception {
method message { 'too small' }
}
class TooBig is Exception {
method message { 'too big' }
}
for TooSmall, TooBig -> $type {
{
$type.new.throw;
CATCH {
when TooSmall { say 'small' }
when TooBig { say 'big' }
}
}
}🦋 You can find the source code in the file two-exceptions.raku.
Output
small
bigComments
The loop throws a
TooSmallon the first pass and aTooBigon the second, each inside its own block with its ownCATCH.The
CATCHphaser has awhenbranch for each exception type. On each pass, only the branch matching the thrown type runs — so the first pass printssmalland the second printsbig. This is how a single set of handlers reacts differently to different kinds of error.