Moose::...ceptions(3)
π NAME π
okay bestie, so this is Moose::Manual::Exceptions β it's the page in the User Contributed Perl Documentation manual that explains Moose's exceptions. like, everything Moose throws at you when stuff goes wrong, and how to catch it without losing your mind. π
β¨ VERSION β¨
this is version 2.4000, no cap. π«Ά
πΈ EXCEPTIONS IN MOOSE πΈ
okay so the whole vibe here: Moose throws an exception for all error conditions. every single one. and that goes for both the code living inside the Moose core and all the code that gets generated when a class is made immutable. so like, everywhere. π
and every single one of those exceptions? subclass of the Moose::Exception class. every type of error gets its own unique subclass, and lots of those subclasses bring extra attributes with them so you get the tea β like, what classes or roles were involved in the mess. π
π¦ EXCEPTION STRINGIFICATION π¦
by default, Moose exceptions remove Moose internals from the stack trace. clean, tidy, iconic. β¨
but β if you set the MOOSE_FULL_EXCEPTION environment variable to a true value, then the Moose internals will be included in the trace. that's it, that's the whole toggle. π
π HANDLING MOOSE EXCEPTIONS π
so because Moose's exceptions use the standard die mechanism, you are free to catch and handle errors however you like. full freedom, main character energy. you could use an eval block to catch Moose exceptions if you want.
but the Moose team strongly recommends using Try::Tiny instead. fr fr. go read Try::Tiny's documentation for the whole discussion of how eval is dangerous. that part is not optional reading, bestie. π
the following example demonstrates how to catch and inspect a Moose::Exception. for the sake of simplicity, we will cause a very simple error. the extends keyword expects a list of superclass names. if we pass no superclass names, Moose will throw an instance of Moose::Exception::ExtendsMissingArgs. β¨
π Catching with Try::Tiny π
use warnings;
use strict;
use Try::Tiny;
try {
package Example::Exception;
use Moose;
extends; # <-- error!
}
catch {
# $_ contains the instance of the exception thrown by the above try
# block, but $_ may get clobbered, so we should copy its value to
# another variable.
my $e = $_;
# Exception objects are not ubiquitous in Perl, so we must check
# whether $e is blessed. We also need to ensure that $e is actually
# the kind of exception we were expecting.
if ( blessed $e
&& $e->isa('Moose::Exception::ExtendsMissingArgs') ) {
my $class_name = $e->class_name;
warn "You forgot to specify a superclass for $class_name, silly!";
}
# It's either another type of an object or not an object at all.
else {
warn "$e\n";
}
};
π Example of catching ValidationFailedForTypeConstraint π
use warnings;
use strict;
use Try::Tiny;
{
package Person;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'NameStr',
as 'Str',
where { $_ =~ /^[a-zA-Z]+$/; };
has age => (
is => 'ro',
isa => 'Int',
required => 1
);
has name => (
is => 'ro',
isa => 'NameStr',
required => 1
);
}
my $person;
while ( !$person ) {
try {
print 'Enter your age : ';
my $age = <STDIN>;
chomp $age;
print 'Enter your name : ';
my $name = <STDIN>;
chomp $name;
$person = Person->new(
age => $age,
name => $name
);
my $person_name = $person->name;
my $person_age = $person->age;
print "$person_name is $person_age years old\n";
}
catch {
my $e = $_;
if (
blessed $e
&& $e->isa(
'Moose::Exception::ValidationFailedForTypeConstraint')
) {
my $attribute_name = $e->attribute->name;
my $type_name = $e->type->name;
my $value = $e->value;
warn
"You entered $value for $attribute_name, which is not a $type_name!";
}
else {
warn "$e\n";
}
};
}
π Example of catching AttributeIsRequired π
use warnings;
use strict;
use Try::Tiny;
{
package Example::RequiredAttribute;
use Moose;
has required_attribute => (
is => 'ro',
isa => 'Int',
required => 1
);
}
try {
# we're not passing required_attribute, so it'll throw an exception
my $object = Example::RequiredAttribute->new();
}
catch {
my $e = $_;
if ( blessed $e && $e->isa('Moose::Exception::AttributeIsRequired') )
{
warn $e->message, "\n";
}
else {
warn "$e\n";
}
};
π MOOSE EXCEPTION CLASSES π
all the exception classes are listed in Moose::Manual::Exceptions::Manifest. go look, it's the full roster, the whole squad. π
π«Ά AUTHORS π«Ά
these are the legends who made it happen:
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Jesse Luehrs <doy@cpan.org>
- Shawn M Moore <sartak@cpan.org>
- ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>
- Karen Etheridge <ether@cpan.org>
- Florian Ragwitz <rafl@debian.org>
- Hans Dieter Pearcey <hdp@cpan.org>
- Chris Prather <chris@prather.org>
- Matt S Trout <mstrout@cpan.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2006 by Infinity Interactive, Inc. π
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. periodt. π
okay that's the whole page bestie, you ate it up π
β this was version 2.4000 of Moose::Manual::Exceptions from the User Contributed Perl Documentation manual, and it shipped with perl v5.42.0 on 2025-07-25. go forth and catch those exceptions. π¦πβ¨
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms