Error with a custom message
is Exception
X::AdHoc
is the type into which objects are wrapped if they are thrown as exceptions, but don't inherit from Exception.
Its benefit over returning non-Exception
objects is that it gives access to all the methods from class Exception, like backtrace
and rethrow
.
You can obtain the original object with the payload
method.
tryprint "Got HTTP code ",$!.payload[0], # 404" and backtrace ",$!.backtrace.Str;
Note that young code will often be prototyped using X::AdHoc
and then later be revised to use more specific subtypes of Exception
. As such it is usually best not to explicitly rely on receiving an X::AdHoc
– in many cases using the string returned by the .message
method, which all Exception
s must have, is preferable. Please note that we need to explicitly call .Str
to stringify the backtrace correctly.
Returns the original object which was passed to die
.
method Numeric()
Converts the payload to Numeric
and returns it
method from-slurpy (|cap)
Creates a new exception from a capture and returns it. The capture will have the SlurpySentry
role mixed in, so that the .message
method behaves in a different when printing the message.
try;print $!.payload.^name; # OUTPUT: «Capture+{X::AdHoc::SlurpySentry}»print $!.message; # OUTPUT: «3FalseNot here»
The SlurpySentry
role joins the elements of the payload, instead of directly converting them to a string.