class Exception

class Exception { ... }

All exceptions that are placed into the $! variable (or into $_ in CATCH blocks) inherit from Exception. When you call die or fail with a non-Exception argument, it is wrapped into an X::AdHoc object, which also inherits from Exception.

User-defined exception classes should inherit from Exception too, and define at least a method message.

class X::YourApp::SomeError is Exception {
    method message() {
        "A YourApp-Specific error occurred: out of coffee!";
    }
}

Methods

method message

method message(Exception:D:) returns Str:D

This is a stub that must be overwritten by subclasses, and should return the exception message.

Special care should be taken that this method does not produce an exception itself.

method backtrace

method backtrace(Exception:D:) returns Backtrace:D

Returns the backtrace associated with the exception. Only makes sense on exceptions that have been thrown at least once.

method throw

method throw(Exception:D:)

Throws the exception.

method rethrow

method rethrow(Exception:D:)

Rethrows an exception that has already been thrown at least once. This is different from throw in that it preserves the original backtrace.

method fail

method fail(Exception:D:)

Same as fail $exception; i.e., it exits the calling Routine and returns the exception wrapped in a Failure object.

method gist

multi method gist(Exception:D:)

Returns whatever the exception printer should produce for this exception. The default implementation returns message and backtrace separated by a newline.

Type graph

Below you should see an imgage showing the type relations for Exception. If not, try the PNG version.

perl6-type-graph X::AdHoc X::AdHoc Exception Exception X::AdHoc->Exception X::Comp X::Comp X::Comp->Exception X::Anon::Augment X::Anon::Augment X::Anon::Augment->X::Comp X::Anon::Augment->Exception X::Anon::Multi X::Anon::Multi X::Anon::Multi->X::Comp X::Anon::Multi->Exception X::Assignment::RO X::Assignment::RO X::Assignment::RO->Exception X::Attribute::NoPackage X::Attribute::NoPackage X::Attribute::NoPackage->X::Comp X::Attribute::NoPackage->Exception X::Attribute::Package X::Attribute::Package X::Attribute::Package->X::Comp X::Attribute::Package->Exception X::Undeclared X::Undeclared X::Undeclared->X::Comp X::Undeclared->Exception X::Attribute::Undeclared X::Attribute::Undeclared X::Attribute::Undeclared->X::Undeclared X::Augment::NoSuchType X::Augment::NoSuchType X::Augment::NoSuchType->X::Comp X::Augment::NoSuchType->Exception X::Bind X::Bind X::Bind->Exception X::Bind::NativeType X::Bind::NativeType X::Bind::NativeType->X::Comp X::Bind::NativeType->Exception X::Bind::Slice X::Bind::Slice X::Bind::Slice->Exception X::Bind::ZenSlice X::Bind::ZenSlice X::Bind::ZenSlice->X::Bind::Slice X::Buf::AsStr X::Buf::AsStr X::Buf::AsStr->Exception X::Buf::Pack X::Buf::Pack X::Buf::Pack->Exception X::Buf::Pack::NonASCII X::Buf::Pack::NonASCII X::Buf::Pack::NonASCII->Exception X::Comp::AdHoc X::Comp::AdHoc X::Comp::AdHoc->X::AdHoc X::Comp::AdHoc->X::Comp X::Comp::AdHoc->Exception X::NYI X::NYI X::NYI->Exception X::Comp::NYI X::Comp::NYI X::Comp::NYI->X::Comp X::Comp::NYI->X::NYI X::Comp::NYI->Exception X::Composition::NotComposable X::Composition::NotComposable X::Composition::NotComposable->Exception X::Constructor::Positional X::Constructor::Positional X::Constructor::Positional->Exception X::ControlFlow X::ControlFlow X::ControlFlow->Exception X::ControlFlow::Return X::ControlFlow::Return X::ControlFlow::Return->X::ControlFlow X::Temporal X::Temporal X::Temporal->Exception X::DateTime::TimezoneClash X::DateTime::TimezoneClash X::DateTime::TimezoneClash->X::Temporal X::Declaration::Scope X::Declaration::Scope X::Declaration::Scope->X::Comp X::Declaration::Scope->Exception X::Declaration::Scope::Multi X::Declaration::Scope::Multi X::Declaration::Scope::Multi->X::Declaration::Scope X::Does::TypeObject X::Does::TypeObject X::Does::TypeObject->Exception X::Eval::NoSuchLang X::Eval::NoSuchLang X::Eval::NoSuchLang->Exception X::Export::NameClash X::Export::NameClash X::Export::NameClash->Exception X::Hash::Store::OddNumber X::Hash::Store::OddNumber X::Hash::Store::OddNumber->Exception X::HyperOp::NonDWIM X::HyperOp::NonDWIM X::HyperOp::NonDWIM->Exception X::OS X::OS X::IO X::IO X::IO->X::OS X::IO::Chdir X::IO::Chdir X::IO::Chdir->X::IO X::IO::Chdir->Exception X::IO::Chmod X::IO::Chmod X::IO::Chmod->X::IO X::IO::Chmod->Exception X::IO::Copy X::IO::Copy X::IO::Copy->X::IO X::IO::Copy->Exception X::IO::Cwd X::IO::Cwd X::IO::Cwd->X::IO X::IO::Cwd->Exception X::IO::Dir X::IO::Dir X::IO::Dir->X::IO X::IO::Dir->Exception X::IO::Mkdir X::IO::Mkdir X::IO::Mkdir->X::IO X::IO::Mkdir->Exception X::IO::Rename X::IO::Rename X::IO::Rename->X::IO X::IO::Rename->Exception X::IO::Rmdir X::IO::Rmdir X::IO::Rmdir->X::IO X::IO::Rmdir->Exception X::IO::Unlink X::IO::Unlink X::IO::Unlink->X::IO X::IO::Unlink->Exception X::Import::MissingSymbols X::Import::MissingSymbols X::Import::MissingSymbols->Exception X::Inheritance::Unsupported X::Inheritance::Unsupported X::Inheritance::Unsupported->Exception X::Localizer::NoContainer X::Localizer::NoContainer X::Localizer::NoContainer->Exception X::Method::InvalidQualifier X::Method::InvalidQualifier X::Method::InvalidQualifier->Exception X::Method::NotFound X::Method::NotFound X::Method::NotFound->Exception X::Method::Private::Permission X::Method::Private::Permission X::Method::Private::Permission->X::Comp X::Method::Private::Permission->Exception X::Method::Private::Unqualified X::Method::Private::Unqualified X::Method::Private::Unqualified->X::Comp X::Method::Private::Unqualified->Exception X::Mixin::NotComposable X::Mixin::NotComposable X::Mixin::NotComposable->Exception X::NoDispatcher X::NoDispatcher X::NoDispatcher->Exception X::Numeric::Real X::Numeric::Real X::Numeric::Real->Exception X::Obsolete X::Obsolete X::Obsolete->X::Comp X::Obsolete->X::Comp X::Obsolete->Exception X::Obsolete->Exception X::OutOfRange X::OutOfRange X::OutOfRange->Exception X::Package::Stubbed X::Package::Stubbed X::Package::Stubbed->X::Comp X::Package::Stubbed->Exception X::Parameter::Default X::Parameter::Default X::Parameter::Default->X::Comp X::Parameter::Default->Exception X::Parameter::MultipleTypeConstraints X::Parameter::MultipleTypeConstraints X::Parameter::MultipleTypeConstraints->X::Comp X::Parameter::MultipleTypeConstraints->Exception X::Parameter::Placeholder X::Parameter::Placeholder X::Parameter::Placeholder->X::Comp X::Parameter::Placeholder->Exception X::Parameter::Twigil X::Parameter::Twigil X::Parameter::Twigil->X::Comp X::Parameter::Twigil->Exception X::Parameter::WrongOrder X::Parameter::WrongOrder X::Parameter::WrongOrder->X::Comp X::Parameter::WrongOrder->Exception X::Phaser::Multiple X::Phaser::Multiple X::Phaser::Multiple->X::Comp X::Phaser::Multiple->Exception X::Phaser::PrePost X::Phaser::PrePost X::Phaser::PrePost->Exception X::Placeholder::Block X::Placeholder::Block X::Placeholder::Block->X::Comp X::Placeholder::Block->Exception X::Placeholder::Mainline X::Placeholder::Mainline X::Placeholder::Mainline->X::Placeholder::Block X::Pod X::Pod X::Redeclaration X::Redeclaration X::Redeclaration->X::Comp X::Redeclaration->Exception X::Role::Initialization X::Role::Initialization X::Role::Initialization->Exception X::Routine::Unwrap X::Routine::Unwrap X::Routine::Unwrap->Exception X::Sequence::Deduction X::Sequence::Deduction X::Sequence::Deduction->Exception X::Set::Coerce X::Set::Coerce X::Set::Coerce->Exception X::Signature::NameClash X::Signature::NameClash X::Signature::NameClash->X::Comp X::Signature::NameClash->Exception X::Signature::Placeholder X::Signature::Placeholder X::Signature::Placeholder->X::Comp X::Signature::Placeholder->Exception X::Str::Match::x X::Str::Match::x X::Str::Match::x->Exception X::Str::Numeric X::Str::Numeric X::Str::Numeric->Exception X::Str::Trans::IllegalKey X::Str::Trans::IllegalKey X::Str::Trans::IllegalKey->Exception X::Str::Trans::InvalidArg X::Str::Trans::InvalidArg X::Str::Trans::InvalidArg->Exception X::Syntax X::Syntax X::Syntax->X::Comp X::Syntax->Exception X::Syntax::Augment::Role X::Syntax::Augment::Role X::Syntax::Augment::Role->X::Syntax X::Syntax::Augment::Role->Exception X::Syntax::Augment::WithoutMonkeyTyping X::Syntax::Augment::WithoutMonkeyTyping X::Syntax::Augment::WithoutMonkeyTyping->X::Syntax X::Syntax::Augment::WithoutMonkeyTyping->Exception X::Syntax::Comment::Embedded X::Syntax::Comment::Embedded X::Syntax::Comment::Embedded->X::Syntax X::Syntax::Comment::Embedded->Exception X::Syntax::Confused X::Syntax::Confused X::Syntax::Confused->X::Syntax X::Syntax::Confused->Exception X::Syntax::Extension::Category X::Syntax::Extension::Category X::Syntax::Extension::Category->X::Syntax X::Syntax::Extension::Category->Exception X::Syntax::InfixInTermPosition X::Syntax::InfixInTermPosition X::Syntax::InfixInTermPosition->X::Syntax X::Syntax::InfixInTermPosition->Exception X::Syntax::Malformed X::Syntax::Malformed X::Syntax::Malformed->X::Syntax X::Syntax::Malformed->Exception X::Syntax::Missing X::Syntax::Missing X::Syntax::Missing->X::Syntax X::Syntax::Missing->Exception X::Syntax::Name::Null X::Syntax::Name::Null X::Syntax::Name::Null->X::Syntax X::Syntax::Name::Null->Exception X::Syntax::NegatedPair X::Syntax::NegatedPair X::Syntax::NegatedPair->X::Syntax X::Syntax::NegatedPair->Exception X::Syntax::NoSelf X::Syntax::NoSelf X::Syntax::NoSelf->X::Syntax X::Syntax::NoSelf->Exception X::Syntax::Number::RadixOutOfRange X::Syntax::Number::RadixOutOfRange X::Syntax::Number::RadixOutOfRange->X::Syntax X::Syntax::Number::RadixOutOfRange->Exception X::Syntax::P5 X::Syntax::P5 X::Syntax::P5->X::Syntax X::Syntax::P5->Exception X::Syntax::Pod::BeginWithoutEnd X::Syntax::Pod::BeginWithoutEnd X::Syntax::Pod::BeginWithoutEnd->X::Pod X::Syntax::Pod::BeginWithoutEnd->X::Syntax X::Syntax::Pod::BeginWithoutEnd->Exception X::Syntax::Pod::BeginWithoutIdentifier X::Syntax::Pod::BeginWithoutIdentifier X::Syntax::Pod::BeginWithoutIdentifier->X::Pod X::Syntax::Pod::BeginWithoutIdentifier->X::Syntax X::Syntax::Pod::BeginWithoutIdentifier->Exception X::Syntax::Regex::Adverb X::Syntax::Regex::Adverb X::Syntax::Regex::Adverb->X::Syntax X::Syntax::Regex::Adverb->Exception X::Syntax::Reserved X::Syntax::Reserved X::Syntax::Reserved->X::Syntax X::Syntax::Reserved->Exception X::Syntax::Self::WithoutObject X::Syntax::Self::WithoutObject X::Syntax::Self::WithoutObject->X::Syntax X::Syntax::Self::WithoutObject->Exception X::Syntax::SigilWithoutName X::Syntax::SigilWithoutName X::Syntax::SigilWithoutName->X::Syntax X::Syntax::SigilWithoutName->Exception X::Syntax::Signature::InvocantMarker X::Syntax::Signature::InvocantMarker X::Syntax::Signature::InvocantMarker->X::Syntax X::Syntax::Signature::InvocantMarker->Exception X::Syntax::UnlessElse X::Syntax::UnlessElse X::Syntax::UnlessElse->X::Syntax X::Syntax::UnlessElse->Exception X::Syntax::Variable::IndirectDeclaration X::Syntax::Variable::IndirectDeclaration X::Syntax::Variable::IndirectDeclaration->X::Syntax X::Syntax::Variable::IndirectDeclaration->Exception X::Syntax::Variable::Match X::Syntax::Variable::Match X::Syntax::Variable::Match->X::Syntax X::Syntax::Variable::Match->Exception X::Syntax::Variable::Numeric X::Syntax::Variable::Numeric X::Syntax::Variable::Numeric->X::Syntax X::Syntax::Variable::Numeric->Exception X::Syntax::Variable::Twigil X::Syntax::Variable::Twigil X::Syntax::Variable::Twigil->X::Syntax X::Syntax::Variable::Twigil->Exception X::Syntax::VirtualCall X::Syntax::VirtualCall X::Syntax::VirtualCall->X::Syntax X::Syntax::VirtualCall->Exception X::Temporal::InvalidFormat X::Temporal::InvalidFormat X::Temporal::InvalidFormat->X::Temporal X::Temporal::Truncation X::Temporal::Truncation X::Temporal::Truncation->X::Temporal X::TypeCheck X::TypeCheck X::TypeCheck->Exception X::TypeCheck::Assignment X::TypeCheck::Assignment X::TypeCheck::Assignment->X::TypeCheck X::TypeCheck::Binding X::TypeCheck::Binding X::TypeCheck::Binding->X::TypeCheck X::TypeCheck::Return X::TypeCheck::Return X::TypeCheck::Return->X::TypeCheck X::TypeCheck::Splice X::TypeCheck::Splice X::TypeCheck::Splice->X::TypeCheck X::Value::Dynamic X::Value::Dynamic X::Value::Dynamic->X::Comp X::Value::Dynamic->Exception Any Any Exception->Any Mu Mu Any->Mu

Methods supplied by class Any

Exception inherits from class Any, which provides the following methods:

method ACCEPTS

multi method ACCEPTS(Any:D: Mu $other)

Returns True if $other === self (i.e. it checks object identity).

method any

Interprets the invocant as a list and creates an any-Junction from it.

method all

Interprets the invocant as a list and creates an all-Junction from it.

method one

Interprets the invocant as a list and creates an one-Junction from it.

method none

Interprets the invocant as a list and creates an none-Junction from it.

Methods supplied by class Mu

Exception inherits from class Mu, which provides the following methods:

method Str

multi method Str()   returns Str

Returns a string representation of the invocant, intended to be machine readable.

method clone

method clone(*%twiddles)

Creates a shallow clone of the invocant. If named arguments are passed to it, their values are used in every place where an attribute name matches the name of a named argument.

method new

multi method new(*%attrinit)

Default method for constructing (create + initialize) new objects of a class. This method expects only named arguments which are then used to initialize attributes with accessors of the same name.

Classes may provide their own new method to override this default.

method bless

method bless(*%attrinit) returns Mu:D

Lower-level object construction method than new.

Creates a new object of the same type as the invocant, uses the named arguments to initialize attributes, and returns the created object.

You can use this method when writing custom constructors:

class Point {
    has $.x;
    has $.y;
    multi method new($x, $y) {
        self.bless(:$x, :$y);
    }
}
my $p = Point.new(-1, 1);

(Though each time you write a custom constructor, remember that it makes subclassing harder).

method CREATE

method CREATE() returns Mu:D

Allocates a new object of the same type as the invocant, without initializing any attributes.

method print

multi method print() returns Bool:D

Prints value to $*OUT after stringification using .Str method without newline at end.

method say

multi method say() returns Bool:D

Prints value to $*OUT after stringification using .gist method with newline at end.

method ACCEPTS

multi method ACCEPTS(Mu:U: $other)

Performs a type check. Returns True if $other conforms to the invocant (which is always a type object or failure).

This is the method that is triggered on smart-matching against type objects, for example in if $var ~~ Int { ... }.

method WHICH

multi method WHICH() returns ObjAt:D

Returns an object of type ObjAt which uniquely identifies the object. Value types override this method which makes sure that two equivalent objects return the same return value from WHICH.