{
    "mode": "perldoc",
    "parameter": "Moose::Manual::Exceptions",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AManual%3A%3AExceptions/json",
    "generated": "2026-06-12T19:15:44Z",
    "sections": {
        "NAME": {
            "content": "Moose::Manual::Exceptions - Moose's exceptions\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "EXCEPTIONS IN MOOSE": {
            "content": "Moose will throw an exception for all error conditions. This applies both to code in the Moose\ncore *as well* as to all code generated when a class is made immutable. All exceptions are\nsubclasses of the \"Moose::Exception\" class.\n\nEach type of error has its own unique subclass, and many subclasses have additional attributes\nto provide more information about the error's context, such as what classes or roles were\ninvolved.\n",
            "subsections": []
        },
        "EXCEPTION STRINGIFICATION": {
            "content": "By default, Moose exceptions remove Moose internals from the stack trace. If you set the\n\"MOOSEFULLEXCEPTION\" environment variable to a true value, then the Moose internals will be\nincluded in the trace.\n",
            "subsections": []
        },
        "HANDLING MOOSE EXCEPTIONS": {
            "content": "Because Moose's exceptions use the standard \"die\" mechanism, you are free to catch and handle\nerrors however you like. You could use an \"eval\" block to catch Moose exceptions. However, the\nMoose team strongly recommends using Try::Tiny instead. Please refer to Try::Tiny's\ndocumentation for a discussion of how \"eval\" is dangerous.\n\nThe following example demonstrates how to catch and inspect a Moose::Exception. For the sake of\nsimplicity, we will cause a very simple error. The \"extends\" keywords expects a list of\nsuperclass names. If we pass no superclass names, Moose will throw an instance of\nMoose::Exception::ExtendsMissingArgs.\n",
            "subsections": [
                {
                    "name": "Catching with Try::Tiny",
                    "content": "use warnings;\nuse strict;\nuse Try::Tiny;\n\ntry {\npackage Example::Exception;\nuse Moose;\nextends;    # <-- error!\n}\ncatch {\n# $ contains the instance of the exception thrown by the above try\n# block, but $ may get clobbered, so we should copy its value to\n# another variable.\nmy $e = $;\n\n# Exception objects are not ubiquitous in Perl, so we must check\n# whether $e is blessed. We also need to ensure that $e is actually\n# the kind of exception we were expecting.\nif ( blessed $e\n&& $e->isa('Moose::Exception::ExtendsMissingArgs') ) {\n\nmy $classname = $e->classname;\nwarn \"You forgot to specify a superclass for $classname, silly!\";\n}\n\n# It's either another type of an object or not an object at all.\nelse {\nwarn \"$e\\n\";\n}\n};\n"
                },
                {
                    "name": "Example of catching ValidationFailedForTypeConstraint",
                    "content": "use warnings;\nuse strict;\n\nuse Try::Tiny;\n\n{\npackage Person;\nuse Moose;\nuse Moose::Util::TypeConstraints;\n\nsubtype 'NameStr',\nas 'Str',\nwhere { $ =~ /^[a-zA-Z]+$/; };\n\nhas age => (\nis       => 'ro',\nisa      => 'Int',\nrequired => 1\n);\n\nhas name => (\nis       => 'ro',\nisa      => 'NameStr',\nrequired => 1\n);\n}\n\nmy $person;\nwhile ( !$person ) {\ntry {\nprint 'Enter your age : ';\nmy $age = <STDIN>;\nchomp $age;\nprint 'Enter your name : ';\nmy $name = <STDIN>;\nchomp $name;\n$person = Person->new(\nage  => $age,\nname => $name\n);\nmy $personname = $person->name;\nmy $personage  = $person->age;\nprint \"$personname is $personage years old\\n\";\n}\ncatch {\nmy $e = $;\n\nif (\nblessed $e\n&& $e->isa(\n'Moose::Exception::ValidationFailedForTypeConstraint')\n) {\n\nmy $attributename = $e->attribute->name;\nmy $typename      = $e->type->name;\nmy $value          = $e->value;\n\nwarn\n\"You entered $value for $attributename, which is not a $typename!\";\n}\nelse {\nwarn \"$e\\n\";\n}\n};\n}\n"
                },
                {
                    "name": "Example of catching AttributeIsRequired",
                    "content": "use warnings;\nuse strict;\nuse Try::Tiny;\n\n{\npackage Example::RequiredAttribute;\nuse Moose;\n\nhas requiredattribute => (\nis       => 'ro',\nisa      => 'Int',\nrequired => 1\n);\n}\n\ntry {\n# we're not passing requiredattribute, so it'll throw an exception\nmy $object = Example::RequiredAttribute->new();\n}\ncatch {\nmy $e = $;\nif ( blessed $e && $e->isa('Moose::Exception::AttributeIsRequired') )\n{\nwarn $e->message, \"\\n\";\n}\nelse {\nwarn \"$e\\n\";\n}\n};\n"
                }
            ]
        },
        "MOOSE EXCEPTION CLASSES": {
            "content": "All the exception classes are listed in Moose::Manual::Exceptions::Manifest.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   Stevan Little <stevan@cpan.org>\n\n*   Dave Rolsky <autarch@urth.org>\n\n*   Jesse Luehrs <doy@cpan.org>\n\n*   Shawn M Moore <sartak@cpan.org>\n\n*   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>\n\n*   Karen Etheridge <ether@cpan.org>\n\n*   Florian Ragwitz <rafl@debian.org>\n\n*   Hans Dieter Pearcey <hdp@cpan.org>\n\n*   Chris Prather <chris@prather.org>\n\n*   Matt S Trout <mstrout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2006 by Infinity Interactive, Inc.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Moose::Manual::Exceptions - Moose's exceptions",
    "flags": [],
    "examples": [],
    "see_also": []
}