class X::Cannot::Empty
class X::Cannot::Empty is Exception { }
Error, typically wrapped in a Failure, when inappropriately using an empty collection.
For example, the following stack implementation fails when trying to pop a value from an empty stack. Sink context causes the returned Failure to throw.
class Stack {
my class Node {
has $.value;
has Node $.next;
}
has Node $!next;
method push($value) {
$!next .= new(:$value, :$!next);
self;
}
method pop() {
fail X::Cannot::Empty.new(:action<pop>, :what(self.^name))
unless $!next;
my $value = $!next.value;
$!next .= next;
$value;
}
}
my $stack = Stack.new.push(42);
say $stack.pop; # OUTPUT: Ā«42ā¤Ā»
try $stack.pop;
say $!.message; # OUTPUT: Ā«Cannot pop from an empty Stackā¤Ā»
Methods
method action
method action()
Verbal description of the inappropriate action.
method what
method what()
Returns the type that was the target of the action.