See Original text in context
multi sub fail(Exception )method fail(Exception:)
Exits the calling Routine
and returns a Failure object wrapping the exception.
# A custom exception definedis Exceptionsub say-word ( )my = say-word("foo");say .exception;
The routine form works in the same way, with an alternative syntax: fail ForbiddenWord.new(:word($word))
.
See Original text in context
method fail(Channel: )
Closes the Channel
(that is, makes subsequent send
calls die), and enqueues the error to be thrown as the final element in the channel. Method receive
will throw that error as an exception. Does nothing if the channel has already been closed or .fail
has already been called on it.
my = Channel.new;.fail("Bad error happens!");.receive;CATCH ;# OUTPUT: «X::AdHoc: Bad error happens!»
See Original text in context
multi sub fail(--> Nil)multi sub fail(*)multi sub fail(Exception --> Nil )multi sub fail( --> Nil)multi sub fail(|cap (*) --> Nil)multi sub fail(Failure --> Nil)multi sub fail(Failure --> Nil)
Exits the calling Routine
and returns a Failure object wrapping the exception $e
- or, for the cap
or $payload
form, an X::AdHoc exception constructed from the concatenation of @text
. If the caller activated fatal exceptions via the pragma use fatal;
, the exception is thrown instead of being returned as a Failure
.
# A custom exception definedis Exceptionsub copy-directory-tree ()# A Failure with X::AdHoc exception object is returned and# assigned, so no throwing Would be thrown without an assignmentmy = copy-directory-tree("cat.jpg");say .exception; # OUTPUT: «cat.jpg is not a directory»# A Failure with a custom Exception object is returned= copy-directory-tree('foo');say .exception; # OUTPUT: «This directory is forbidden: 'foo'»
If it's called with a generic Failure
, an ad-hoc undefined failure is thrown; if it's a defined Failure
, it will be marked as unhandled.
sub re-failmy = re-fail;say .handled; # OUTPUT: «False»