autodie-2.06_01 > autodie::exception

名前

autodie::exception - Exceptions from autodying functions.

autodie::exception - autodie した関数の例外

概要

    eval {
        use autodie;

        open(my $fh, '<', 'some_file.txt');

        ...
    };

    if (my $E = $@) {
        say "Ooops!  ",$E->caller," had problems: $@";
    }

説明

When an autodie enabled function fails, it generates an autodie::exception object. This can be interrogated to determine further information about the error that occurred.

When an autodie enabled function fails, it generates an autodie::exception object. This can be interrogated to determine further information about the error that occurred. (TBT)

This document is broken into two sections; those methods that are most useful to the end-developer, and those methods for anyone wishing to subclass or get very familiar with autodie::exception.

This document is broken into two sections; those methods that are most useful to the end-developer, and those methods for anyone wishing to subclass or get very familiar with autodie::exception. (TBT)

Common Methods

These methods are intended to be used in the everyday dealing of exceptions.

These methods are intended to be used in the everyday dealing of exceptions. (TBT)

The following assume that the error has been copied into a separate scalar:

The following assume that the error has been copied into a separate scalar: (TBT)

    if ($E = $@) {
        ...
    }

This is not required, but is recommended in case any code is called which may reset or alter $@.

This is not required, but is recommended in case any code is called which may reset or alter $@. (TBT)

args

    my $array_ref = $E->args;

Provides a reference to the arguments passed to the subroutine that died.

Provides a reference to the arguments passed to the subroutine that died. (TBT)

function

    my $sub = $E->function;

The subroutine (including package) that threw the exception.

The subroutine (including package) that threw the exception. (TBT)

file

    my $file = $E->file;

The file in which the error occurred (eg, myscript.pl or MyTest.pm).

The file in which the error occurred (eg, myscript.pl or MyTest.pm). (TBT)

package

    my $package = $E->package;

The package from which the exceptional subroutine was called.

The package from which the exceptional subroutine was called. (TBT)

caller

    my $caller = $E->caller;

The subroutine that called the exceptional code.

The subroutine that called the exceptional code. (TBT)

line

    my $line = $E->line;

The line in $E->file where the exceptional code was called.

The line in $E->file where the exceptional code was called. (TBT)

context

    my $context = $E->context;

The context in which the subroutine was called. This can be 'list', 'scalar', or undefined (unknown). It will never be 'void', as autodie always captures the return value in one way or another.

The context in which the subroutine was called. This can be 'list', 'scalar', or undefined (unknown). It will never be 'void', as autodie always captures the return value in one way or another. (TBT)

return

    my $return_value = $E->return;

The value(s) returned by the failed subroutine. When the subroutine was called in a list context, this will always be a reference to an array containing the results. When the subroutine was called in a scalar context, this will be the actual scalar returned.

The value(s) returned by the failed subroutine. When the subroutine was called in a list context, this will always be a reference to an array containing the results. When the subroutine was called in a scalar context, this will be the actual scalar returned. (TBT)

errno

    my $errno = $E->errno;

The value of $! at the time when the exception occurred.

The value of $! at the time when the exception occurred. (TBT)

NOTE: This method will leave the main autodie::exception class and become part of a role in the future. You should only call errno for exceptions where $! would reasonably have been set on failure.

NOTE: This method will leave the main autodie::exception class and become part of a role in the future. You should only call errno for exceptions where $! would reasonably have been set on failure. (TBT)

eval_error

    my $old_eval_error = $E->eval_error;

The contents of $@ immediately after autodie triggered an exception. This may be useful when dealing with modules such as Text::Balanced that set (but do not throw) $@ on error.

The contents of $@ immediately after autodie triggered an exception. This may be useful when dealing with modules such as Text::Balanced that set (but do not throw) $@ on error. (TBT)

matches

    if ( $e->matches('open') ) { ... }

    if ( $e ~~ 'open' ) { ... }

matches is used to determine whether a given exception matches a particular role. On Perl 5.10, using smart-match (~~) with an autodie::exception object will use matches underneath.

matches is used to determine whether a given exception matches a particular role. On Perl 5.10, using smart-match (~~) with an autodie::exception object will use matches underneath. (TBT)

An exception is considered to match a string if:

An exception is considered to match a string if: (TBT)

  • For a string not starting with a colon, the string exactly matches the package and subroutine that threw the exception. For example, MyModule::log. If the string does not contain a package name, CORE:: is assumed.

    For a string not starting with a colon, the string exactly matches the package and subroutine that threw the exception. For example, MyModule::log. If the string does not contain a package name, CORE:: is assumed. (TBT)

  • For a string that does start with a colon, if the subroutine throwing the exception does that behaviour. For example, the CORE::open subroutine does :file, :io and :all.

    For a string that does start with a colon, if the subroutine throwing the exception does that behaviour. For example, the CORE::open subroutine does :file, :io and :all. (TBT)

    See "CATEGORIES" in autodie for futher information.

    See "CATEGORIES" in autodie for futher information. (TBT)

Advanced methods

The following methods, while usable from anywhere, are primarily intended for developers wishing to subclass autodie::exception, write code that registers custom error messages, or otherwise work closely with the autodie::exception model.

The following methods, while usable from anywhere, are primarily intended for developers wishing to subclass autodie::exception, write code that registers custom error messages, or otherwise work closely with the autodie::exception model. (TBT)

register

    autodie::exception->register( 'CORE::open' => \&mysub );

The register method allows for the registration of a message handler for a given subroutine. The full subroutine name including the package should be used.

The register method allows for the registration of a message handler for a given subroutine. The full subroutine name including the package should be used. (TBT)

Registered message handlers will receive the autodie::exception object as the first parameter.

Registered message handlers will receive the autodie::exception object as the first parameter. (TBT)

add_file_and_line

    say "Problem occurred",$@->add_file_and_line;

Returns the string at %s line %d, where %s is replaced with the filename, and %d is replaced with the line number.

Returns the string at %s line %d, where %s is replaced with the filename, and %d is replaced with the line number. (TBT)

Primarily intended for use by format handlers.

Primarily intended for use by format handlers. (TBT)

stringify

    say "The error was: ",$@->stringify;

Formats the error as a human readable string. Usually there's no reason to call this directly, as it is used automatically if an autodie::exception object is ever used as a string.

Formats the error as a human readable string. Usually there's no reason to call this directly, as it is used automatically if an autodie::exception object is ever used as a string. (TBT)

Child classes can override this method to change how they're stringified.

Child classes can override this method to change how they're stringified. (TBT)

format_default

    my $error_string = $E->format_default;

This produces the default error string for the given exception, without using any registered message handlers. It is primarily intended to be called from a message handler when they have been passed an exception they don't want to format.

This produces the default error string for the given exception, without using any registered message handlers. It is primarily intended to be called from a message handler when they have been passed an exception they don't want to format. (TBT)

Child classes can override this method to change how default messages are formatted.

Child classes can override this method to change how default messages are formatted. (TBT)

new

    my $error = autodie::exception->new(
        args => \@_,
        function => "CORE::open",
        errno => $!,
        context => 'scalar',
        return => undef,
    );

Creates a new autodie::exception object. Normally called directly from an autodying function. The function argument is required, its the function we were trying to call that generated the exception. The args parameter is optional.

Creates a new autodie::exception object. Normally called directly from an autodying function. The function argument is required, its the function we were trying to call that generated the exception. The args parameter is optional. (TBT)

The errno value is optional. In versions of autodie::exception 1.99 and earlier the code would try to automatically use the current value of $!, but this was unreliable and is no longer supported.

The errno value is optional. In versions of autodie::exception 1.99 and earlier the code would try to automatically use the current value of $!, but this was unreliable and is no longer supported. (TBT)

Atrributes such as package, file, and caller are determined automatically, and cannot be specified.

Atrributes such as package, file, and caller are determined automatically, and cannot be specified. (TBT)

SEE ALSO

autodie, autodie::exception::system

ライセンス

Copyright (C)2008 Paul Fenwick

This is free software. You may modify and/or redistribute this code under the same terms as Perl 5.10 itself, or, at your option, any later version of Perl 5.

作者

Paul Fenwick <[email protected]>