Each type of error has its own unique subclass, and many subclasses have additional attributes to provide more information about the error's context, such as what classes or roles were involved.
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" keywords expects a list of superclass names. If we pass no superclass names, Moose will throw an instance of Moose::Exception::ExtendsMissingArgs.
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"; } }
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"; } } }
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"; } };
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.