.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Moose::Manual::Exceptions 3pm" .TH Moose::Manual::Exceptions 3pm 2024-01-21 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Moose::Manual::Exceptions \- Moose's exceptions .SH VERSION .IX Header "VERSION" version 2.2207 .SH "EXCEPTIONS IN MOOSE" .IX Header "EXCEPTIONS IN MOOSE" Moose will throw an exception for all error conditions. This applies both to code in the Moose core \fIas well\fR as to all code generated when a class is made immutable. All exceptions are subclasses of the \f(CW\*(C`Moose::Exception\*(C'\fR class. .PP 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. .SH "EXCEPTION STRINGIFICATION" .IX Header "EXCEPTION STRINGIFICATION" By default, Moose exceptions remove Moose internals from the stack trace. If you set the \f(CW\*(C`MOOSE_FULL_EXCEPTION\*(C'\fR environment variable to a true value, then the Moose internals will be included in the trace. .SH "HANDLING MOOSE EXCEPTIONS" .IX Header "HANDLING MOOSE EXCEPTIONS" Because Moose's exceptions use the standard \f(CW\*(C`die\*(C'\fR mechanism, you are free to catch and handle errors however you like. You could use an \f(CW\*(C`eval\*(C'\fR block to catch Moose exceptions. However, the Moose team strongly recommends using Try::Tiny instead. Please refer to Try::Tiny's documentation for a discussion of how \f(CW\*(C`eval\*(C'\fR is dangerous. .PP 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 \f(CW\*(C`extends\*(C'\fR keywords expects a list of superclass names. If we pass no superclass names, Moose will throw an instance of Moose::Exception::ExtendsMissingArgs. .SS "Catching with Try::Tiny" .IX Subsection "Catching with Try::Tiny" .Vb 3 \& 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(\*(AqMoose::Exception::ExtendsMissingArgs\*(Aq) ) { \& \& my $class_name = $e\->class_name; \& warn "You forgot to specify a superclass for $class_name, silly!"; \& } \& \& # It\*(Aqs either another type of an object or not an object at all. \& else { \& warn "$e\en"; \& } \& }; .Ve .SS "Example of catching ValidationFailedForTypeConstraint" .IX Subsection "Example of catching ValidationFailedForTypeConstraint" .Vb 2 \& use warnings; \& use strict; \& \& use Try::Tiny; \& \& { \& package Person; \& use Moose; \& use Moose::Util::TypeConstraints; \& \& subtype \*(AqNameStr\*(Aq, \& as \*(AqStr\*(Aq, \& where { $_ =~ /^[a\-zA\-Z]+$/; }; \& \& has age => ( \& is => \*(Aqro\*(Aq, \& isa => \*(AqInt\*(Aq, \& required => 1 \& ); \& \& has name => ( \& is => \*(Aqro\*(Aq, \& isa => \*(AqNameStr\*(Aq, \& required => 1 \& ); \& } \& \& my $person; \& while ( !$person ) { \& try { \& print \*(AqEnter your age : \*(Aq; \& my $age = ; \& chomp $age; \& print \*(AqEnter your name : \*(Aq; \& my $name = ; \& 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\en"; \& } \& catch { \& my $e = $_; \& \& if ( \& blessed $e \& && $e\->isa( \& \*(AqMoose::Exception::ValidationFailedForTypeConstraint\*(Aq) \& ) { \& \& 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\en"; \& } \& }; \& } .Ve .SS "Example of catching AttributeIsRequired" .IX Subsection "Example of catching AttributeIsRequired" .Vb 3 \& use warnings; \& use strict; \& use Try::Tiny; \& \& { \& package Example::RequiredAttribute; \& use Moose; \& \& has required_attribute => ( \& is => \*(Aqro\*(Aq, \& isa => \*(AqInt\*(Aq, \& required => 1 \& ); \& } \& \& try { \& # we\*(Aqre not passing required_attribute, so it\*(Aqll throw an exception \& my $object = Example::RequiredAttribute\->new(); \& } \& catch { \& my $e = $_; \& if ( blessed $e && $e\->isa(\*(AqMoose::Exception::AttributeIsRequired\*(Aq) ) \& { \& warn $e\->message, "\en"; \& } \& else { \& warn "$e\en"; \& } \& }; .Ve .SH "MOOSE EXCEPTION CLASSES" .IX Header "MOOSE EXCEPTION CLASSES" All the exception classes are listed in Moose::Manual::Exceptions::Manifest. .SH AUTHORS .IX Header "AUTHORS" .IP \(bu 4 Stevan Little .IP \(bu 4 Dave Rolsky .IP \(bu 4 Jesse Luehrs .IP \(bu 4 Shawn M Moore .IP \(bu 4 יובל קוג'מן (Yuval Kogman) .IP \(bu 4 Karen Etheridge .IP \(bu 4 Florian Ragwitz .IP \(bu 4 Hans Dieter Pearcey .IP \(bu 4 Chris Prather .IP \(bu 4 Matt S Trout .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2006 by Infinity Interactive, Inc. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.