PageRenderTime 62ms CodeModel.GetById 15ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/extlib/man/man3/Error.3pm

https://bitbucket.org/tharris/wormbase-gbrowse1
Unknown | 517 lines | 517 code | 0 blank | 0 comment | 0 complexity | b933917b8213d29460a5e1e69b7f8f37 MD5 | raw file
  1.\" Automatically generated by Pod::Man 2.1801 (Pod::Simple 3.05)
  2.\"
  3.\" Standard preamble:
  4.\" ========================================================================
  5.de Sp \" Vertical space (when we can't use .PP)
  6.if t .sp .5v
  7.if n .sp
  8..
  9.de Vb \" Begin verbatim text
 10.ft CW
 11.nf
 12.ne \\$1
 13..
 14.de Ve \" End verbatim text
 15.ft R
 16.fi
 17..
 18.\" Set up some character translations and predefined strings.  \*(-- will
 19.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
 20.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
 21.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
 22.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
 23.\" nothing in troff, for use with C<>.
 24.tr \(*W-
 25.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
 26.ie n \{\
 27.    ds -- \(*W-
 28.    ds PI pi
 29.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
 30.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
 31.    ds L" ""
 32.    ds R" ""
 33.    ds C` ""
 34.    ds C' ""
 35'br\}
 36.el\{\
 37.    ds -- \|\(em\|
 38.    ds PI \(*p
 39.    ds L" ``
 40.    ds R" ''
 41'br\}
 42.\"
 43.\" Escape single quotes in literal strings from groff's Unicode transform.
 44.ie \n(.g .ds Aq \(aq
 45.el       .ds Aq '
 46.\"
 47.\" If the F register is turned on, we'll generate index entries on stderr for
 48.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
 49.\" entries marked with X<> in POD.  Of course, you'll have to process the
 50.\" output yourself in some meaningful fashion.
 51.ie \nF \{\
 52.    de IX
 53.    tm Index:\\$1\t\\n%\t"\\$2"
 54..
 55.    nr % 0
 56.    rr F
 57.\}
 58.el \{\
 59.    de IX
 60..
 61.\}
 62.\"
 63.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
 64.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
 65.    \" fudge factors for nroff and troff
 66.if n \{\
 67.    ds #H 0
 68.    ds #V .8m
 69.    ds #F .3m
 70.    ds #[ \f1
 71.    ds #] \fP
 72.\}
 73.if t \{\
 74.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
 75.    ds #V .6m
 76.    ds #F 0
 77.    ds #[ \&
 78.    ds #] \&
 79.\}
 80.    \" simple accents for nroff and troff
 81.if n \{\
 82.    ds ' \&
 83.    ds ` \&
 84.    ds ^ \&
 85.    ds , \&
 86.    ds ~ ~
 87.    ds /
 88.\}
 89.if t \{\
 90.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
 91.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
 92.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
 93.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
 94.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
 95.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
 96.\}
 97.    \" troff and (daisy-wheel) nroff accents
 98.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
 99.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105.ds ae a\h'-(\w'a'u*4/10)'e
106.ds Ae A\h'-(\w'A'u*4/10)'E
107.    \" corrections for vroff
108.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110.    \" for low resolution devices (crt and lpr)
111.if \n(.H>23 .if \n(.V>19 \
112\{\
113.    ds : e
114.    ds 8 ss
115.    ds o a
116.    ds d- d\h'-1'\(ga
117.    ds D- D\h'-1'\(hy
118.    ds th \o'bp'
119.    ds Th \o'LP'
120.    ds ae ae
121.    ds Ae AE
122.\}
123.rm #[ #] #H #V #F C
124.\" ========================================================================
125.\"
126.IX Title "Error 3pm"
127.TH Error 3pm "2010-05-08" "perl v5.10.0" "User Contributed Perl Documentation"
128.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129.\" way too many mistakes in technical documents.
130.if n .ad l
131.nh
132.SH "NAME"
133Error \- Error/exception handling in an OO\-ish way
134.SH "WARNING"
135.IX Header "WARNING"
136Using the \*(L"Error\*(R" module is \fBno longer recommended\fR due to the black-magical
137nature of its syntactic sugar, which often tends to break. Its maintainers 
138have stopped actively writing code that uses it, and discourage people
139from doing so. See the \*(L"\s-1SEE\s0 \s-1ALSO\s0\*(R" section below for better recommendations.
140.SH "SYNOPSIS"
141.IX Header "SYNOPSIS"
142.Vb 1
143\&    use Error qw(:try);
144\&
145\&    throw Error::Simple( "A simple error");
146\&
147\&    sub xyz {
148\&        ...
149\&        record Error::Simple("A simple error")
150\&            and return;
151\&    }
152\& 
153\&    unlink($file) or throw Error::Simple("$file: $!",$!);
154\&
155\&    try {
156\&        do_some_stuff();
157\&        die "error!" if $condition;
158\&        throw Error::Simple "Oops!" if $other_condition;
159\&    }
160\&    catch Error::IO with {
161\&        my $E = shift;
162\&        print STDERR "File ", $E\->{\*(Aq\-file\*(Aq}, " had a problem\en";
163\&    }
164\&    except {
165\&        my $E = shift;
166\&        my $general_handler=sub {send_message $E\->{\-description}};
167\&        return {
168\&            UserException1 => $general_handler,
169\&            UserException2 => $general_handler
170\&        };
171\&    }
172\&    otherwise {
173\&        print STDERR "Well I don\*(Aqt know what to say\en";
174\&    }
175\&    finally {
176\&        close_the_garage_door_already(); # Should be reliable
177\&    }; # Don\*(Aqt forget the trailing ; or you might be surprised
178.Ve
179.SH "DESCRIPTION"
180.IX Header "DESCRIPTION"
181The \f(CW\*(C`Error\*(C'\fR package provides two interfaces. Firstly \f(CW\*(C`Error\*(C'\fR provides
182a procedural interface to exception handling. Secondly \f(CW\*(C`Error\*(C'\fR is a
183base class for errors/exceptions that can either be thrown, for
184subsequent catch, or can simply be recorded.
185.PP
186Errors in the class \f(CW\*(C`Error\*(C'\fR should not be thrown directly, but the
187user should throw errors from a sub-class of \f(CW\*(C`Error\*(C'\fR.
188.SH "PROCEDURAL INTERFACE"
189.IX Header "PROCEDURAL INTERFACE"
190\&\f(CW\*(C`Error\*(C'\fR exports subroutines to perform exception handling. These will
191be exported if the \f(CW\*(C`:try\*(C'\fR tag is used in the \f(CW\*(C`use\*(C'\fR line.
192.IP "try \s-1BLOCK\s0 \s-1CLAUSES\s0" 4
193.IX Item "try BLOCK CLAUSES"
194\&\f(CW\*(C`try\*(C'\fR is the main subroutine called by the user. All other subroutines
195exported are clauses to the try subroutine.
196.Sp
197The \s-1BLOCK\s0 will be evaluated and, if no error is throw, try will return
198the result of the block.
199.Sp
200\&\f(CW\*(C`CLAUSES\*(C'\fR are the subroutines below, which describe what to do in the
201event of an error being thrown within \s-1BLOCK\s0.
202.IP "catch \s-1CLASS\s0 with \s-1BLOCK\s0" 4
203.IX Item "catch CLASS with BLOCK"
204This clauses will cause all errors that satisfy \f(CW\*(C`$err\->isa(CLASS)\*(C'\fR
205to be caught and handled by evaluating \f(CW\*(C`BLOCK\*(C'\fR.
206.Sp
207\&\f(CW\*(C`BLOCK\*(C'\fR will be passed two arguments. The first will be the error
208being thrown. The second is a reference to a scalar variable. If this
209variable is set by the catch block then, on return from the catch
210block, try will continue processing as if the catch block was never
211found. The error will also be available in \f(CW$@\fR.
212.Sp
213To propagate the error the catch block may call \f(CW\*(C`$err\->throw\*(C'\fR
214.Sp
215If the scalar reference by the second argument is not set, and the
216error is not thrown. Then the current try block will return with the
217result from the catch block.
218.IP "except \s-1BLOCK\s0" 4
219.IX Item "except BLOCK"
220When \f(CW\*(C`try\*(C'\fR is looking for a handler, if an except clause is found
221\&\f(CW\*(C`BLOCK\*(C'\fR is evaluated. The return value from this block should be a
222\&\s-1HASHREF\s0 or a list of key-value pairs, where the keys are class names
223and the values are \s-1CODE\s0 references for the handler of errors of that
224type.
225.IP "otherwise \s-1BLOCK\s0" 4
226.IX Item "otherwise BLOCK"
227Catch any error by executing the code in \f(CW\*(C`BLOCK\*(C'\fR
228.Sp
229When evaluated \f(CW\*(C`BLOCK\*(C'\fR will be passed one argument, which will be the
230error being processed. The error will also be available in \f(CW$@\fR.
231.Sp
232Only one otherwise block may be specified per try block
233.IP "finally \s-1BLOCK\s0" 4
234.IX Item "finally BLOCK"
235Execute the code in \f(CW\*(C`BLOCK\*(C'\fR either after the code in the try block has
236successfully completed, or if the try block throws an error then
237\&\f(CW\*(C`BLOCK\*(C'\fR will be executed after the handler has completed.
238.Sp
239If the handler throws an error then the error will be caught, the
240finally block will be executed and the error will be re-thrown.
241.Sp
242Only one finally block may be specified per try block
243.SH "COMPATIBILITY"
244.IX Header "COMPATIBILITY"
245Moose exports a keyword called \f(CW\*(C`with\*(C'\fR which clashes with Error's. This
246example returns a prototype mismatch error:
247.PP
248.Vb 1
249\&    package MyTest;
250\&
251\&    use warnings;
252\&    use Moose;
253\&    use Error qw(:try);
254.Ve
255.PP
256(Thanks to \f(CW\*(C`maik.hentsche@amd.com\*(C'\fR for the report.).
257.SH "CLASS INTERFACE"
258.IX Header "CLASS INTERFACE"
259.SS "\s-1CONSTRUCTORS\s0"
260.IX Subsection "CONSTRUCTORS"
261The \f(CW\*(C`Error\*(C'\fR object is implemented as a \s-1HASH\s0. This \s-1HASH\s0 is initialized
262with the arguments that are passed to it's constructor. The elements
263that are used by, or are retrievable by the \f(CW\*(C`Error\*(C'\fR class are listed
264below, other classes may add to these.
265.PP
266.Vb 5
267\&        \-file
268\&        \-line
269\&        \-text
270\&        \-value
271\&        \-object
272.Ve
273.PP
274If \f(CW\*(C`\-file\*(C'\fR or \f(CW\*(C`\-line\*(C'\fR are not specified in the constructor arguments
275then these will be initialized with the file name and line number where
276the constructor was called from.
277.PP
278If the error is associated with an object then the object should be
279passed as the \f(CW\*(C`\-object\*(C'\fR argument. This will allow the \f(CW\*(C`Error\*(C'\fR package
280to associate the error with the object.
281.PP
282The \f(CW\*(C`Error\*(C'\fR package remembers the last error created, and also the
283last error associated with a package. This could either be the last
284error created by a sub in that package, or the last error which passed
285an object blessed into that package as the \f(CW\*(C`\-object\*(C'\fR argument.
286.IP "Error\->\fInew()\fR" 4
287.IX Item "Error->new()"
288See the Error::Simple documentation.
289.IP "throw ( [ \s-1ARGS\s0 ] )" 4
290.IX Item "throw ( [ ARGS ] )"
291Create a new \f(CW\*(C`Error\*(C'\fR object and throw an error, which will be caught
292by a surrounding \f(CW\*(C`try\*(C'\fR block, if there is one. Otherwise it will cause
293the program to exit.
294.Sp
295\&\f(CW\*(C`throw\*(C'\fR may also be called on an existing error to re-throw it.
296.IP "with ( [ \s-1ARGS\s0 ] )" 4
297.IX Item "with ( [ ARGS ] )"
298Create a new \f(CW\*(C`Error\*(C'\fR object and returns it. This is defined for
299syntactic sugar, eg
300.Sp
301.Vb 1
302\&    die with Some::Error ( ... );
303.Ve
304.IP "record ( [ \s-1ARGS\s0 ] )" 4
305.IX Item "record ( [ ARGS ] )"
306Create a new \f(CW\*(C`Error\*(C'\fR object and returns it. This is defined for
307syntactic sugar, eg
308.Sp
309.Vb 2
310\&    record Some::Error ( ... )
311\&        and return;
312.Ve
313.SS "\s-1STATIC\s0 \s-1METHODS\s0"
314.IX Subsection "STATIC METHODS"
315.IP "prior ( [ \s-1PACKAGE\s0 ] )" 4
316.IX Item "prior ( [ PACKAGE ] )"
317Return the last error created, or the last error associated with
318\&\f(CW\*(C`PACKAGE\*(C'\fR
319.IP "flush ( [ \s-1PACKAGE\s0 ] )" 4
320.IX Item "flush ( [ PACKAGE ] )"
321Flush the last error created, or the last error associated with
322\&\f(CW\*(C`PACKAGE\*(C'\fR.It is necessary to clear the error stack before exiting the
323package or uncaught errors generated using \f(CW\*(C`record\*(C'\fR will be reported.
324.Sp
325.Vb 1
326\&     $Error\->flush;
327.Ve
328.SS "\s-1OBJECT\s0 \s-1METHODS\s0"
329.IX Subsection "OBJECT METHODS"
330.IP "stacktrace" 4
331.IX Item "stacktrace"
332If the variable \f(CW$Error::Debug\fR was non-zero when the error was
333created, then \f(CW\*(C`stacktrace\*(C'\fR returns a string created by calling
334\&\f(CW\*(C`Carp::longmess\*(C'\fR. If the variable was zero the \f(CW\*(C`stacktrace\*(C'\fR returns
335the text of the error appended with the filename and line number of
336where the error was created, providing the text does not end with a
337newline.
338.IP "object" 4
339.IX Item "object"
340The object this error was associated with
341.IP "file" 4
342.IX Item "file"
343The file where the constructor of this error was called from
344.IP "line" 4
345.IX Item "line"
346The line where the constructor of this error was called from
347.IP "text" 4
348.IX Item "text"
349The text of the error
350.ie n .IP "$err\->associate($obj)" 4
351.el .IP "\f(CW$err\fR\->associate($obj)" 4
352.IX Item "$err->associate($obj)"
353Associates an error with an object to allow error propagation. I.e:
354.Sp
355.Vb 2
356\&    $ber\->encode(...) or
357\&        return Error\->prior($ber)\->associate($ldap);
358.Ve
359.SS "\s-1OVERLOAD\s0 \s-1METHODS\s0"
360.IX Subsection "OVERLOAD METHODS"
361.IP "stringify" 4
362.IX Item "stringify"
363A method that converts the object into a string. This method may simply
364return the same as the \f(CW\*(C`text\*(C'\fR method, or it may append more
365information. For example the file name and line number.
366.Sp
367By default this method returns the \f(CW\*(C`\-text\*(C'\fR argument that was passed to
368the constructor, or the string \f(CW"Died"\fR if none was given.
369.IP "value" 4
370.IX Item "value"
371A method that will return a value that can be associated with the
372error. For example if an error was created due to the failure of a
373system call, then this may return the numeric value of \f(CW$!\fR at the
374time.
375.Sp
376By default this method returns the \f(CW\*(C`\-value\*(C'\fR argument that was passed
377to the constructor.
378.SH "PRE-DEFINED ERROR CLASSES"
379.IX Header "PRE-DEFINED ERROR CLASSES"
380.SS "Error::Simple"
381.IX Subsection "Error::Simple"
382This class can be used to hold simple error strings and values. It's
383constructor takes two arguments. The first is a text value, the second
384is a numeric value. These values are what will be returned by the
385overload methods.
386.PP
387If the text value ends with \f(CW\*(C`at file line 1\*(C'\fR as $@ strings do, then
388this infomation will be used to set the \f(CW\*(C`\-file\*(C'\fR and \f(CW\*(C`\-line\*(C'\fR arguments
389of the error object.
390.PP
391This class is used internally if an eval'd block die's with an error
392that is a plain string. (Unless \f(CW$Error::ObjectifyCallback\fR is modified)
393.ie n .SH "$Error::ObjectifyCallback"
394.el .SH "\f(CW$Error::ObjectifyCallback\fP"
395.IX Header "$Error::ObjectifyCallback"
396This variable holds a reference to a subroutine that converts errors that
397are plain strings to objects. It is used by Error.pm to convert textual
398errors to objects, and can be overrided by the user.
399.PP
400It accepts a single argument which is a hash reference to named parameters. 
401Currently the only named parameter passed is \f(CW\*(Aqtext\*(Aq\fR which is the text
402of the error, but others may be available in the future.
403.PP
404For example the following code will cause Error.pm to throw objects of the
405class MyError::Bar by default:
406.PP
407.Vb 7
408\&    sub throw_MyError_Bar
409\&    {
410\&        my $args = shift;
411\&        my $err = MyError::Bar\->new();
412\&        $err\->{\*(AqMyBarText\*(Aq} = $args\->{\*(Aqtext\*(Aq};
413\&        return $err;
414\&    }
415\&
416\&    {
417\&        local $Error::ObjectifyCallback = \e&throw_MyError_Bar;
418\&
419\&        # Error handling here.
420\&    }
421.Ve
422.SH "MESSAGE HANDLERS"
423.IX Header "MESSAGE HANDLERS"
424\&\f(CW\*(C`Error\*(C'\fR also provides handlers to extend the output of the \f(CW\*(C`warn()\*(C'\fR perl
425function, and to handle the printing of a thrown \f(CW\*(C`Error\*(C'\fR that is not caught
426or otherwise handled. These are not installed by default, but are requested
427using the \f(CW\*(C`:warndie\*(C'\fR tag in the \f(CW\*(C`use\*(C'\fR line.
428.PP
429.Vb 1
430\& use Error qw( :warndie );
431.Ve
432.PP
433These new error handlers are installed in \f(CW$SIG{_\|_WARN_\|_}\fR and
434\&\f(CW$SIG{_\|_DIE_\|_}\fR. If these handlers are already defined when the tag is
435imported, the old values are stored, and used during the new code. Thus, to
436arrange for custom handling of warnings and errors, you will need to perform
437something like the following:
438.PP
439.Vb 5
440\& BEGIN {
441\&   $SIG{_\|_WARN_\|_} = sub {
442\&     print STDERR "My special warning handler: $_[0]"
443\&   };
444\& }
445\&
446\& use Error qw( :warndie );
447.Ve
448.PP
449Note that setting \f(CW$SIG{_\|_WARN_\|_}\fR after the \f(CW\*(C`:warndie\*(C'\fR tag has been
450imported will overwrite the handler that \f(CW\*(C`Error\*(C'\fR provides. If this cannot be
451avoided, then the tag can be explicitly \f(CW\*(C`import\*(C'\fRed later
452.PP
453.Vb 1
454\& use Error;
455\&
456\& $SIG{_\|_WARN_\|_} = ...;
457\&
458\& import Error qw( :warndie );
459.Ve
460.SS "\s-1EXAMPLE\s0"
461.IX Subsection "EXAMPLE"
462The \f(CW\*(C`_\|_DIE_\|_\*(C'\fR handler turns messages such as
463.PP
464.Vb 1
465\& Can\*(Aqt call method "foo" on an undefined value at examples/warndie.pl line 16.
466.Ve
467.PP
468into
469.PP
470.Vb 1
471\& Unhandled perl error caught at toplevel:
472\&
473\&   Can\*(Aqt call method "foo" on an undefined value
474\&
475\& Thrown from: examples/warndie.pl:16
476\&
477\& Full stack trace:
478\&
479\&         main::inner(\*(Aqundef\*(Aq) called at examples/warndie.pl line 20
480\&         main::outer(\*(Aqundef\*(Aq) called at examples/warndie.pl line 23
481.Ve
482.SH "SEE ALSO"
483.IX Header "SEE ALSO"
484See Exception::Class for a different module providing Object-Oriented
485exception handling, along with a convenient syntax for declaring hierarchies
486for them. It doesn't provide Error's syntactic sugar of \f(CW\*(C`try { ... }\*(C'\fR,
487\&\f(CW\*(C`catch { ... }\*(C'\fR, etc. which may be a good thing or a bad thing based
488on what you want. (Because Error's syntactic sugar tends to break.)
489.PP
490Error::Exception aims to combine Error and Exception::Class
491\&\*(L"with correct stringification\*(R".
492.PP
493TryCatch and Try::Tiny are similar in concept to Error.pm only providing 
494a syntax that hopefully breaks less.
495.SH "KNOWN BUGS"
496.IX Header "KNOWN BUGS"
497None, but that does not mean there are not any.
498.SH "AUTHORS"
499.IX Header "AUTHORS"
500Graham Barr <gbarr@pobox.com>
501.PP
502The code that inspired me to write this was originally written by
503Peter Seibel <peter@weblogic.com> and adapted by Jesse Glick
504<jglick@sig.bsh.com>.
505.PP
506\&\f(CW\*(C`:warndie\*(C'\fR handlers added by Paul Evans <leonerd@leonerd.org.uk>
507.SH "MAINTAINER"
508.IX Header "MAINTAINER"
509Shlomi Fish <shlomif@iglu.org.il>
510.SH "PAST MAINTAINERS"
511.IX Header "PAST MAINTAINERS"
512Arun Kumar U <u_arunkumar@yahoo.com>
513.SH "COPYRIGHT"
514.IX Header "COPYRIGHT"
515Copyright (c) 1997\-8  Graham Barr. All rights reserved.
516This program is free software; you can redistribute it and/or modify it
517under the same terms as Perl itself.