In Exception§
See primary documentation in context for routine fail
multi 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))
.
In Channel§
See primary documentation in context for method fail
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!»
In Failure§
See primary documentation in context for sub fail
multi fail(--> Nil)multi fail(*)multi fail(Exception --> Nil )multi fail( --> Nil)multi fail(|cap (*) --> Nil)multi fail(Failure --> Nil)multi 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»