class X::AdHoc 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.

try {
    die [404'File not found']; # throw non-exception object 
}
print "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 Exceptions must have, is preferable. Please note that we need to explicitly call .Str to stringify the backtrace correctly.

Methods§

method payload§

Returns the original object which was passed to die.

method Numeric§

method Numeric()

Converts the payload to Numeric and returns it

method from-slurpy§

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 {
    X::AdHoc.from-slurpy3False"Not here" ).throw
};
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.