1Moose::Manual::ExceptioUnsse(r3)Contributed Perl DocumenMtoaotsieo:n:Manual::Exceptions(3)
2
3
4
6 Moose::Manual::Exceptions - Moose's exceptions
7
9 version 2.2201
10
12 Moose will throw an exception for all error conditions. This applies
13 both to code in the Moose core as well as to all code generated when a
14 class is made immutable. All exceptions are subclasses of the
15 "Moose::Exception" class.
16
17 Each type of error has its own unique subclass, and many subclasses
18 have additional attributes to provide more information about the
19 error's context, such as what classes or roles were involved.
20
22 By default, Moose exceptions remove Moose internals from the stack
23 trace. If you set the "MOOSE_FULL_EXCEPTION" environment variable to a
24 true value, then the Moose internals will be included in the trace.
25
27 Because Moose's exceptions use the standard "die" mechanism, you are
28 free to catch and handle errors however you like. You could use an
29 "eval" block to catch Moose exceptions. However, the Moose team
30 strongly recommends using Try::Tiny instead. Please refer to
31 Try::Tiny's documentation for a discussion of how "eval" is dangerous.
32
33 The following example demonstrates how to catch and inspect a
34 Moose::Exception. For the sake of simplicity, we will cause a very
35 simple error. The "extends" keywords expects a list of superclass
36 names. If we pass no superclass names, Moose will throw an instance of
37 Moose::Exception::ExtendsMissingArgs.
38
39 Catching with Try::Tiny
40 use warnings;
41 use strict;
42 use Try::Tiny;
43
44 try {
45 package Example::Exception;
46 use Moose;
47 extends; # <-- error!
48 }
49 catch {
50 # $_ contains the instance of the exception thrown by the above try
51 # block, but $_ may get clobbered, so we should copy its value to
52 # another variable.
53 my $e = $_;
54
55 # Exception objects are not ubiquitous in Perl, so we must check
56 # whether $e is blessed. We also need to ensure that $e is actually
57 # the kind of exception we were expecting.
58 if ( blessed $e
59 && $e->isa('Moose::Exception::ExtendsMissingArgs') ) {
60
61 my $class_name = $e->class_name;
62 warn "You forgot to specify a superclass for $class_name, silly!";
63 }
64
65 # It's either another type of an object or not an object at all.
66 else {
67 warn "$e\n";
68 }
69 };
70
71 Example of catching ValidationFailedForTypeConstraint
72 use warnings;
73 use strict;
74
75 use Try::Tiny;
76
77 {
78 package Person;
79 use Moose;
80 use Moose::Util::TypeConstraints;
81
82 subtype 'NameStr',
83 as 'Str',
84 where { $_ =~ /^[a-zA-Z]+$/; };
85
86 has age => (
87 is => 'ro',
88 isa => 'Int',
89 required => 1
90 );
91
92 has name => (
93 is => 'ro',
94 isa => 'NameStr',
95 required => 1
96 );
97 }
98
99 my $person;
100 while ( !$person ) {
101 try {
102 print 'Enter your age : ';
103 my $age = <STDIN>;
104 chomp $age;
105 print 'Enter your name : ';
106 my $name = <STDIN>;
107 chomp $name;
108 $person = Person->new(
109 age => $age,
110 name => $name
111 );
112 my $person_name = $person->name;
113 my $person_age = $person->age;
114 print "$person_name is $person_age years old\n";
115 }
116 catch {
117 my $e = $_;
118
119 if (
120 blessed $e
121 && $e->isa(
122 'Moose::Exception::ValidationFailedForTypeConstraint')
123 ) {
124
125 my $attribute_name = $e->attribute->name;
126 my $type_name = $e->type->name;
127 my $value = $e->value;
128
129 warn
130 "You entered $value for $attribute_name, which is not a $type_name!";
131 }
132 else {
133 warn "$e\n";
134 }
135 };
136 }
137
138 Example of catching AttributeIsRequired
139 use warnings;
140 use strict;
141 use Try::Tiny;
142
143 {
144 package Example::RequiredAttribute;
145 use Moose;
146
147 has required_attribute => (
148 is => 'ro',
149 isa => 'Int',
150 required => 1
151 );
152 }
153
154 try {
155 # we're not passing required_attribute, so it'll throw an exception
156 my $object = Example::RequiredAttribute->new();
157 }
158 catch {
159 my $e = $_;
160 if ( blessed $e && $e->isa('Moose::Exception::AttributeIsRequired') )
161 {
162 warn $e->message, "\n";
163 }
164 else {
165 warn "$e\n";
166 }
167 };
168
170 All the exception classes are listed in
171 Moose::Manual::Exceptions::Manifest.
172
174 • Stevan Little <stevan@cpan.org>
175
176 • Dave Rolsky <autarch@urth.org>
177
178 • Jesse Luehrs <doy@cpan.org>
179
180 • Shawn M Moore <sartak@cpan.org>
181
182 • יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
183
184 • Karen Etheridge <ether@cpan.org>
185
186 • Florian Ragwitz <rafl@debian.org>
187
188 • Hans Dieter Pearcey <hdp@cpan.org>
189
190 • Chris Prather <chris@prather.org>
191
192 • Matt S Trout <mstrout@cpan.org>
193
195 This software is copyright (c) 2006 by Infinity Interactive, Inc.
196
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.
199
200
201
202perl v5.36.0 2022-07-22 Moose::Manual::Exceptions(3)