{
    "mode": "perldoc",
    "parameter": "autodie::exception",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/autodie%3A%3Aexception/json",
    "generated": "2026-06-03T02:35:12Z",
    "synopsis": "eval {\nuse autodie;\nopen(my $fh, '<', 'somefile.txt');\n...\n};\nif (my $E = $@) {\nsay \"Ooops!  \",$E->caller,\" had problems: $@\";\n}",
    "sections": {
        "NAME": {
            "content": "autodie::exception - Exceptions from autodying functions.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "eval {\nuse autodie;\n\nopen(my $fh, '<', 'somefile.txt');\n\n...\n};\n\nif (my $E = $@) {\nsay \"Ooops!  \",$E->caller,\" had problems: $@\";\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "When an autodie enabled function fails, it generates an \"autodie::exception\" object. This can be\ninterrogated to determine further information about the error that occurred.\n\nThis document is broken into two sections; those methods that are most useful to the\nend-developer, and those methods for anyone wishing to subclass or get very familiar with\n\"autodie::exception\".\n",
            "subsections": [
                {
                    "name": "Common Methods",
                    "content": "These methods are intended to be used in the everyday dealing of exceptions.\n\nThe following assume that the error has been copied into a separate scalar:\n\nif ($E = $@) {\n...\n}\n\nThis is not required, but is recommended in case any code is called which may reset or alter $@.\n\nargs\nmy $arrayref = $E->args;\n\nProvides a reference to the arguments passed to the subroutine that died.\n\nfunction\nmy $sub = $E->function;\n\nThe subroutine (including package) that threw the exception.\n\nfile\nmy $file = $E->file;\n\nThe file in which the error occurred (eg, \"myscript.pl\" or \"MyTest.pm\").\n\npackage\nmy $package = $E->package;\n\nThe package from which the exceptional subroutine was called.\n\ncaller\nmy $caller = $E->caller;\n\nThe subroutine that *called* the exceptional code.\n\nline\nmy $line = $E->line;\n\nThe line in \"$E->file\" where the exceptional code was called.\n\ncontext\nmy $context = $E->context;\n\nThe context in which the subroutine was called by autodie; usually the same as the context in\nwhich you called the autodying subroutine. This can be 'list', 'scalar', or undefined (unknown).\nIt will never be 'void', as \"autodie\" always captures the return value in one way or another.\n\nFor some core functions that always return a scalar value regardless of their context (eg,\n\"chown\"), this may be 'scalar', even if you used a list context.\n\nreturn\nmy $returnvalue = $E->return;\n\nThe value(s) returned by the failed subroutine. When the subroutine was called in a list\ncontext, this will always be a reference to an array containing the results. When the subroutine\nwas called in a scalar context, this will be the actual scalar returned.\n\nerrno\nmy $errno = $E->errno;\n\nThe value of $! at the time when the exception occurred.\n\nNOTE: This method will leave the main \"autodie::exception\" class and become part of a role in\nthe future. You should only call \"errno\" for exceptions where $! would reasonably have been set\non failure.\n\nevalerror\nmy $oldevalerror = $E->evalerror;\n\nThe contents of $@ immediately after autodie triggered an exception. This may be useful when\ndealing with modules such as Text::Balanced that set (but do not throw) $@ on error.\n\nmatches\nif ( $e->matches('open') ) { ... }\n\nif ( 'open' ~~ $e ) { ... }\n\n\"matches\" is used to determine whether a given exception matches a particular role.\n\nAn exception is considered to match a string if:\n\n*   For a string not starting with a colon, the string exactly matches the package and\nsubroutine that threw the exception. For example, \"MyModule::log\". If the string does not\ncontain a package name, \"CORE::\" is assumed.\n\n*   For a string that does start with a colon, if the subroutine throwing the exception *does*\nthat behaviour. For example, the \"CORE::open\" subroutine does \":file\", \":io\" and \":all\".\n\nSee \"CATEGORIES\" in autodie for further information.\n\nOn Perl 5.10 and above, using smart-match (\"~~\") with an \"autodie::exception\" object will\nuse \"matches\" underneath. This module used to recommend using smart-match with the exception\nobject on the left hand side, but in future Perls that is likely to stop working. The\nsmart-match facility of this class should only be used with the exception object on the\nright hand side. Having the exception object on the right is both future-proof and portable\nto older Perls, back to 5.10. Beware that this facility can only be relied upon when it is\ncertain that the exception object actually is an \"autodie::exception\" object; it is no more\ncapable than an explicit call to the \"matches\" method.\n"
                },
                {
                    "name": "Advanced methods",
                    "content": "The following methods, while usable from anywhere, are primarily intended for developers wishing\nto subclass \"autodie::exception\", write code that registers custom error messages, or otherwise\nwork closely with the \"autodie::exception\" model.\n\nregister\nautodie::exception->register( 'CORE::open' => \\&mysub );\n\nThe \"register\" method allows for the registration of a message handler for a given subroutine.\nThe full subroutine name including the package should be used.\n\nRegistered message handlers will receive the \"autodie::exception\" object as the first parameter.\n\naddfileandline\nsay \"Problem occurred\",$@->addfileandline;\n\nReturns the string \" at %s line %d\", where %s is replaced with the filename, and %d is replaced\nwith the line number.\n\nPrimarily intended for use by format handlers.\n\nstringify\nsay \"The error was: \",$@->stringify;\n\nFormats the error as a human readable string. Usually there's no reason to call this directly,\nas it is used automatically if an \"autodie::exception\" object is ever used as a string.\n\nChild classes can override this method to change how they're stringified.\n\nformatdefault\nmy $errorstring = $E->formatdefault;\n\nThis produces the default error string for the given exception, *without using any registered\nmessage handlers*. It is primarily intended to be called from a message handler when they have\nbeen passed an exception they don't want to format.\n\nChild classes can override this method to change how default messages are formatted.\n\nnew\nmy $error = autodie::exception->new(\nargs => \\@,\nfunction => \"CORE::open\",\nerrno => $!,\ncontext => 'scalar',\nreturn => undef,\n);\n\nCreates a new \"autodie::exception\" object. Normally called directly from an autodying function.\nThe \"function\" argument is required, its the function we were trying to call that generated the\nexception. The \"args\" parameter is optional.\n\nThe \"errno\" value is optional. In versions of \"autodie::exception\" 1.99 and earlier the code\nwould try to automatically use the current value of $!, but this was unreliable and is no longer\nsupported.\n\nAtrributes such as package, file, and caller are determined automatically, and cannot be\nspecified.\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "autodie, autodie::exception::system\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "Copyright (C)2008 Paul Fenwick\n\nThis is free software. You may modify and/or redistribute this code under the same terms as Perl\n5.10 itself, or, at your option, any later version of Perl 5.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Paul Fenwick <pjf@perltraining.com.au>\n",
            "subsections": []
        }
    },
    "summary": "autodie::exception - Exceptions from autodying functions.",
    "flags": [],
    "examples": [],
    "see_also": []
}