# Moose::Manual::Exceptions - Moose's exceptions - perldoc - [phpMan]

## NAME
    [Moose::Manual::Exceptions](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AManual%3A%3AExceptions/markdown) - Moose's exceptions

## VERSION
    version 2.2207

## EXCEPTIONS IN MOOSE
    Moose will throw an exception for all error conditions. This applies both to code in the Moose
    core *as well* as to all code generated when a class is made immutable. All exceptions are
    subclasses of the "[Moose::Exception](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AException/markdown)" class.

    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.

## EXCEPTION STRINGIFICATION
    By default, Moose exceptions remove Moose internals from the stack trace. If you set the
    "MOOSE_FULL_EXCEPTION" environment variable to a true value, then the Moose internals will be
    included in the trace.

## HANDLING MOOSE EXCEPTIONS
    Because Moose's exceptions use the standard "die" mechanism, you are free to catch and handle
    errors however you like. You could use an "eval" block to catch Moose exceptions. However, the
    Moose team strongly recommends using [Try::Tiny](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown) instead. Please refer to [Try::Tiny](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown)'s
    documentation for a discussion of how "eval" is dangerous.

    The following example demonstrates how to catch and inspect a [Moose::Exception](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AException/markdown). 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](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AException%3A%3AExtendsMissingArgs/markdown).

### Catching with [Try::Tiny](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown)
        use warnings;
        use strict;
        use [Try::Tiny](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown);

        try {
            package [Example::Exception](https://www.chedong.com/phpMan.php/perldoc/Example%3A%3AException/markdown);
            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](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AException%3A%3AExtendsMissingArgs/markdown)') ) {

                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](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown);

        {
            package Person;
            use Moose;
            use [Moose::Util::TypeConstraints](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AUtil%3A%3ATypeConstraints/markdown);

            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](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AException%3A%3AValidationFailedForTypeConstraint/markdown)')
                    ) {

                    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](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown);

        {
            package [Example::RequiredAttribute](https://www.chedong.com/phpMan.php/perldoc/Example%3A%3ARequiredAttribute/markdown);
            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](https://www.chedong.com/phpMan.php/perldoc/Example%3A%3ARequiredAttribute/markdown)->new();
        }
        catch {
            my $e = $_;
            if ( blessed $e && $e->isa('[Moose::Exception::AttributeIsRequired](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AException%3A%3AAttributeIsRequired/markdown)') )
            {
                warn $e->message, "\n";
            }
            else {
                warn "$e\n";
            }
        };

## MOOSE EXCEPTION CLASSES
    All the exception classes are listed in [Moose::Manual::Exceptions::Manifest](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AManual%3A%3AExceptions%3A%3AManifest/markdown).

## AUTHORS
    *   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.

