SEARCH
NEW RPMS
DIRECTORIES
ABOUT
FAQ
VARIOUS
BLOG
DONATE


YUM REPOSITORY

 
 

MAN page from Scientific Linux Other perl-Error-0.17015-4.1.noarch.rpm

Error

Section: User Contributed Perl Documentation (3)
Updated: 2012-01-30
Index 

NAME

Error - Error/exception handling in an OO-ish way 

SYNOPSIS

    use Error qw(:try);    throw Error::Simple( "A simple error");    sub xyz {        ...        record Error::Simple("A simple error")            and return;    }     unlink($file) or throw Error::Simple("$file: $!",$!);    try {        do_some_stuff();        die "error!" if $condition;        throw Error::Simple "Oops!" if $other_condition;    }    catch Error::IO with {        my $E = shift;        print STDERR "File ", $E->{'-file'}, " had a problem\n";    }    except {        my $E = shift;        my $general_handler=sub {send_message $E->{-description}};        return {            UserException1 => $general_handler,            UserException2 => $general_handler        };    }    otherwise {        print STDERR "Well I don't know what to say\n";    }    finally {        close_the_garage_door_already(); # Should be reliable    }; # Don't forget the trailing ; or you might be surprised
 

DESCRIPTION

The "Error" package provides two interfaces. Firstly "Error" providesa procedural interface to exception handling. Secondly "Error" is abase class for errors/exceptions that can either be thrown, forsubsequent catch, or can simply be recorded.

Errors in the class "Error" should not be thrown directly, but theuser should throw errors from a sub-class of "Error". 

PROCEDURAL INTERFACE

"Error" exports subroutines to perform exception handling. These willbe exported if the ":try" tag is used in the "use" line.
try BLOCK CLAUSES
"try" is the main subroutine called by the user. All other subroutinesexported are clauses to the try subroutine.

The BLOCK will be evaluated and, if no error is throw, try will returnthe result of the block.

"CLAUSES" are the subroutines below, which describe what to do in theevent of an error being thrown within BLOCK.

catch CLASS with BLOCK
This clauses will cause all errors that satisfy "$err->isa(CLASS)"to be caught and handled by evaluating "BLOCK".

"BLOCK" will be passed two arguments. The first will be the errorbeing thrown. The second is a reference to a scalar variable. If thisvariable is set by the catch block then, on return from the catchblock, try will continue processing as if the catch block was neverfound. The error will also be available in $@.

To propagate the error the catch block may call "$err->throw"

If the scalar reference by the second argument is not set, and theerror is not thrown. Then the current try block will return with theresult from the catch block.

except BLOCK
When "try" is looking for a handler, if an except clause is found"BLOCK" is evaluated. The return value from this block should be aHASHREF or a list of key-value pairs, where the keys are class namesand the values are CODE references for the handler of errors of thattype.
otherwise BLOCK
Catch any error by executing the code in "BLOCK"

When evaluated "BLOCK" will be passed one argument, which will be theerror being processed. The error will also be available in $@.

Only one otherwise block may be specified per try block

finally BLOCK
Execute the code in "BLOCK" either after the code in the try block hassuccessfully completed, or if the try block throws an error then"BLOCK" will be executed after the handler has completed.

If the handler throws an error then the error will be caught, thefinally block will be executed and the error will be re-thrown.

Only one finally block may be specified per try block

 

CLASS INTERFACE

 

CONSTRUCTORS

The "Error" object is implemented as a HASH. This HASH is initializedwith the arguments that are passed to it's constructor. The elementsthat are used by, or are retrievable by the "Error" class are listedbelow, other classes may add to these.

        -file        -line        -text        -value        -object

If "-file" or "-line" are not specified in the constructor argumentsthen these will be initialized with the file name and line number wherethe constructor was called from.

If the error is associated with an object then the object should bepassed as the "-object" argument. This will allow the "Error" packageto associate the error with the object.

The "Error" package remembers the last error created, and also thelast error associated with a package. This could either be the lasterror created by a sub in that package, or the last error which passedan object blessed into that package as the "-object" argument.

Error->new()
See the Error::Simple documentation.
throw ( [ ARGS ] )
Create a new "Error" object and throw an error, which will be caughtby a surrounding "try" block, if there is one. Otherwise it will causethe program to exit.

"throw" may also be called on an existing error to re-throw it.

with ( [ ARGS ] )
Create a new "Error" object and returns it. This is defined forsyntactic sugar, eg

    die with Some::Error ( ... );
record ( [ ARGS ] )
Create a new "Error" object and returns it. This is defined forsyntactic sugar, eg

    record Some::Error ( ... )        and return;
 

STATIC METHODS

prior ( [ PACKAGE ] )
Return the last error created, or the last error associated with"PACKAGE"
flush ( [ PACKAGE ] )
Flush the last error created, or the last error associated with"PACKAGE".It is necessary to clear the error stack before exiting thepackage or uncaught errors generated using "record" will be reported.

     $Error->flush;
 

OBJECT METHODS

stacktrace
If the variable $Error::Debug was non-zero when the error wascreated, then "stacktrace" returns a string created by calling"Carp::longmess". If the variable was zero the "stacktrace" returnsthe text of the error appended with the filename and line number ofwhere the error was created, providing the text does not end with anewline.
object
The object this error was associated with
file
The file where the constructor of this error was called from
line
The line where the constructor of this error was called from
text
The text of the error
$err->associate($obj)
Associates an error with an object to allow error propagation. I.e:

    $ber->encode(...) or        return Error->prior($ber)->associate($ldap);
 

OVERLOAD METHODS

stringify
A method that converts the object into a string. This method may simplyreturn the same as the "text" method, or it may append moreinformation. For example the file name and line number.

By default this method returns the "-text" argument that was passed tothe constructor, or the string "Died" if none was given.

value
A method that will return a value that can be associated with theerror. For example if an error was created due to the failure of asystem call, then this may return the numeric value of $! at thetime.

By default this method returns the "-value" argument that was passedto the constructor.

 

PRE-DEFINED ERROR CLASSES

 

Error::Simple

This class can be used to hold simple error strings and values. It'sconstructor takes two arguments. The first is a text value, the secondis a numeric value. These values are what will be returned by theoverload methods.

If the text value ends with "at file line 1" as $@ strings do, thenthis infomation will be used to set the "-file" and "-line" argumentsof the error object.

This class is used internally if an eval'd block die's with an errorthat is a plain string. (Unless $Error::ObjectifyCallback is modified) 

$Error::ObjectifyCallback

This variable holds a reference to a subroutine that converts errors thatare plain strings to objects. It is used by Error.pm to convert textualerrors to objects, and can be overrided by the user.

It accepts a single argument which is a hash reference to named parameters. Currently the only named parameter passed is 'text' which is the textof the error, but others may be available in the future.

For example the following code will cause Error.pm to throw objects of theclass MyError::Bar by default:

    sub throw_MyError_Bar    {        my $args = shift;        my $err = MyError::Bar->new();        $err->{'MyBarText'} = $args->{'text'};        return $err;    }    {        local $Error::ObjectifyCallback = \&throw_MyError_Bar;        # Error handling here.    }
 

MESSAGE HANDLERS

"Error" also provides handlers to extend the output of the "warn()" perlfunction, and to handle the printing of a thrown "Error" that is not caughtor otherwise handled. These are not installed by default, but are requestedusing the ":warndie" tag in the "use" line.

 use Error qw( :warndie );

These new error handlers are installed in $SIG{__WARN__} and$SIG{__DIE__}. If these handlers are already defined when the tag isimported, the old values are stored, and used during the new code. Thus, toarrange for custom handling of warnings and errors, you will need to performsomething like the following:

 BEGIN {   $SIG{__WARN__} = sub {     print STDERR "My special warning handler: $_[0]"   }; } use Error qw( :warndie );

Note that setting $SIG{__WARN__} after the ":warndie" tag has beenimported will overwrite the handler that "Error" provides. If this cannot beavoided, then the tag can be explicitly "import"ed later

 use Error; $SIG{__WARN__} = ...; import Error qw( :warndie );
 

EXAMPLE

The "__DIE__" handler turns messages such as

 Can't call method "foo" on an undefined value at examples/warndie.pl line 16.

into

 Unhandled perl error caught at toplevel:   Can't call method "foo" on an undefined value Thrown from: examples/warndie.pl:16 Full stack trace:         main::inner('undef') called at examples/warndie.pl line 20         main::outer('undef') called at examples/warndie.pl line 23
 

SEE ALSO

See Exception::Class for a different module providing Object-Orientedexception handling, along with a convenient syntax for declaring hierarchiesfor them. It doesn't provide Error's syntactic sugar of "try { ... }","catch { ... }", etc. which may be a good thing or a bad thing basedon what you want. (Because Error's syntactic sugar tends to break.)

Error::Exception aims to combine Error and Exception::Class``with correct stringification''. 

KNOWN BUGS

None, but that does not mean there are not any. 

AUTHORS

Graham Barr <gbarrAATTpobox.com>

The code that inspired me to write this was originally written byPeter Seibel <peterAATTweblogic.com> and adapted by Jesse Glick<jglickAATTsig.bsh.com>.

":warndie" handlers added by Paul Evans <leonerdAATTleonerd.org.uk> 

MAINTAINER

Shlomi Fish <shlomifAATTiglu.org.il> 

PAST MAINTAINERS

Arun Kumar U <u_arunkumarAATTyahoo.com> 

COPYRIGHT

Copyright (c) 1997-8 Graham Barr. All rights reserved.This program is free software; you can redistribute it and/or modify itunder the same terms as Perl itself.


 

Index

NAME
SYNOPSIS
DESCRIPTION
PROCEDURAL INTERFACE
CLASS INTERFACE
CONSTRUCTORS
STATIC METHODS
OBJECT METHODS
OVERLOAD METHODS
PRE-DEFINED ERROR CLASSES
Error::Simple
$Error::ObjectifyCallback
MESSAGE HANDLERS
EXAMPLE
SEE ALSO
KNOWN BUGS
AUTHORS
MAINTAINER
PAST MAINTAINERS
COPYRIGHT

This document was created byman2html,using the manual pages.