1 |
wakaba |
1.1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
2 |
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
3 |
|
|
<head> |
4 |
|
|
<title>Error - Error/exception handling in an OO-ish way</title> |
5 |
|
|
<link rel="stylesheet" href="http://suika.fam.cx/www/style/html/pod.css" type="text/css" /> |
6 |
|
|
<link rev="made" href="mailto:admin@suika.fam.cx" /> |
7 |
|
|
</head> |
8 |
|
|
|
9 |
|
|
<body> |
10 |
|
|
|
11 |
|
|
<p><a name="__index__"></a></p> |
12 |
|
|
<!-- INDEX BEGIN --> |
13 |
|
|
|
14 |
|
|
<ul> |
15 |
|
|
|
16 |
|
|
<li><a href="#name">NAME</a></li> |
17 |
|
|
<li><a href="#synopsis">SYNOPSIS</a></li> |
18 |
|
|
<li><a href="#description">DESCRIPTION</a></li> |
19 |
|
|
<li><a href="#procedural_interface">PROCEDURAL INTERFACE</a></li> |
20 |
|
|
<li><a href="#class_interface">CLASS INTERFACE</a></li> |
21 |
|
|
<ul> |
22 |
|
|
|
23 |
|
|
<li><a href="#constructors">CONSTRUCTORS</a></li> |
24 |
|
|
<li><a href="#static_methods">STATIC METHODS</a></li> |
25 |
|
|
<li><a href="#object_methods">OBJECT METHODS</a></li> |
26 |
|
|
<li><a href="#overload_methods">OVERLOAD METHODS</a></li> |
27 |
|
|
</ul> |
28 |
|
|
|
29 |
|
|
<li><a href="#predefined_error_classes">PRE-DEFINED ERROR CLASSES</a></li> |
30 |
|
|
<li><a href="#known_bugs">KNOWN BUGS</a></li> |
31 |
|
|
<li><a href="#authors">AUTHORS</a></li> |
32 |
|
|
<li><a href="#maintainer">MAINTAINER</a></li> |
33 |
|
|
</ul> |
34 |
|
|
<!-- INDEX END --> |
35 |
|
|
|
36 |
|
|
<hr /> |
37 |
|
|
<p> |
38 |
|
|
</p> |
39 |
|
|
<h1><a name="name">NAME</a></h1> |
40 |
|
|
<p>Error - Error/exception handling in an OO-ish way</p> |
41 |
|
|
<p> |
42 |
|
|
</p> |
43 |
|
|
<hr /> |
44 |
|
|
<h1><a name="synopsis">SYNOPSIS</a></h1> |
45 |
|
|
<pre> |
46 |
|
|
use Error qw(:try);</pre> |
47 |
|
|
<pre> |
48 |
|
|
throw Error::Simple( "A simple error");</pre> |
49 |
|
|
<pre> |
50 |
|
|
sub xyz { |
51 |
|
|
... |
52 |
|
|
record Error::Simple("A simple error") |
53 |
|
|
and return; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
unlink($file) or throw Error::Simple("$file: $!",$!);</pre> |
57 |
|
|
<pre> |
58 |
|
|
try { |
59 |
|
|
do_some_stuff(); |
60 |
|
|
die "error!" if $condition; |
61 |
|
|
throw Error::Simple -text => "Oops!" if $other_condition; |
62 |
|
|
} |
63 |
|
|
catch Error::IO with { |
64 |
|
|
my $E = shift; |
65 |
|
|
print STDERR "File ", $E->{'-file'}, " had a problem\n"; |
66 |
|
|
} |
67 |
|
|
except { |
68 |
|
|
my $E = shift; |
69 |
|
|
my $general_handler=sub {send_message $E->{-description}}; |
70 |
|
|
return { |
71 |
|
|
UserException1 => $general_handler, |
72 |
|
|
UserException2 => $general_handler |
73 |
|
|
}; |
74 |
|
|
} |
75 |
|
|
otherwise { |
76 |
|
|
print STDERR "Well I don't know what to say\n"; |
77 |
|
|
} |
78 |
|
|
finally { |
79 |
|
|
close_the_garage_door_already(); # Should be reliable |
80 |
|
|
}; # Don't forget the trailing ; or you might be surprised</pre> |
81 |
|
|
<p> |
82 |
|
|
</p> |
83 |
|
|
<hr /> |
84 |
|
|
<h1><a name="description">DESCRIPTION</a></h1> |
85 |
|
|
<p>The <code>Error</code> package provides two interfaces. Firstly <code>Error</code> provides |
86 |
|
|
a procedural interface to exception handling. Secondly <code>Error</code> is a |
87 |
|
|
base class for errors/exceptions that can either be thrown, for |
88 |
|
|
subsequent catch, or can simply be recorded.</p> |
89 |
|
|
<p>Errors in the class <code>Error</code> should not be thrown directly, but the |
90 |
|
|
user should throw errors from a sub-class of <code>Error</code>.</p> |
91 |
|
|
<p> |
92 |
|
|
</p> |
93 |
|
|
<hr /> |
94 |
|
|
<h1><a name="procedural_interface">PROCEDURAL INTERFACE</a></h1> |
95 |
|
|
<p><code>Error</code> exports subroutines to perform exception handling. These will |
96 |
|
|
be exported if the <code>:try</code> tag is used in the <code>use</code> line.</p> |
97 |
|
|
<dl> |
98 |
|
|
<dt><strong><a name="item_try">try BLOCK CLAUSES</a></strong><br /> |
99 |
|
|
</dt> |
100 |
|
|
<dd> |
101 |
|
|
<a href="#item_try"><code>try</code></a> is the main subroutine called by the user. All other subroutines |
102 |
|
|
exported are clauses to the try subroutine. |
103 |
|
|
</dd> |
104 |
|
|
<dd> |
105 |
|
|
<p>The BLOCK will be evaluated and, if no error is throw, try will return |
106 |
|
|
the result of the block.</p> |
107 |
|
|
</dd> |
108 |
|
|
<dd> |
109 |
|
|
<p><code>CLAUSES</code> are the subroutines below, which describe what to do in the |
110 |
|
|
event of an error being thrown within BLOCK.</p> |
111 |
|
|
</dd> |
112 |
|
|
<p></p> |
113 |
|
|
<dt><strong><a name="item_catch_class_with_block">catch CLASS with BLOCK</a></strong><br /> |
114 |
|
|
</dt> |
115 |
|
|
<dd> |
116 |
|
|
This clauses will cause all errors that satisfy <code>$err->isa(CLASS)</code> |
117 |
|
|
to be caught and handled by evaluating <code>BLOCK</code>. |
118 |
|
|
</dd> |
119 |
|
|
<dd> |
120 |
|
|
<p><code>BLOCK</code> will be passed two arguments. The first will be the error |
121 |
|
|
being thrown. The second is a reference to a scalar variable. If this |
122 |
|
|
variable is set by the catch block then, on return from the catch |
123 |
|
|
block, try will continue processing as if the catch block was never |
124 |
|
|
found.</p> |
125 |
|
|
</dd> |
126 |
|
|
<dd> |
127 |
|
|
<p>To propagate the error the catch block may call <a href="#item_throw"><code>$err->throw</code></a></p> |
128 |
|
|
</dd> |
129 |
|
|
<dd> |
130 |
|
|
<p>If the scalar reference by the second argument is not set, and the |
131 |
|
|
error is not thrown. Then the current try block will return with the |
132 |
|
|
result from the catch block.</p> |
133 |
|
|
</dd> |
134 |
|
|
<p></p> |
135 |
|
|
<dt><strong><a name="item_except">except BLOCK</a></strong><br /> |
136 |
|
|
</dt> |
137 |
|
|
<dd> |
138 |
|
|
When <a href="#item_try"><code>try</code></a> is looking for a handler, if an except clause is found |
139 |
|
|
<code>BLOCK</code> is evaluated. The return value from this block should be a |
140 |
|
|
HASHREF or a list of key-value pairs, where the keys are class names |
141 |
|
|
and the values are CODE references for the handler of errors of that |
142 |
|
|
type. |
143 |
|
|
</dd> |
144 |
|
|
<p></p> |
145 |
|
|
<dt><strong><a name="item_otherwise">otherwise BLOCK</a></strong><br /> |
146 |
|
|
</dt> |
147 |
|
|
<dd> |
148 |
|
|
Catch any error by executing the code in <code>BLOCK</code> |
149 |
|
|
</dd> |
150 |
|
|
<dd> |
151 |
|
|
<p>When evaluated <code>BLOCK</code> will be passed one argument, which will be the |
152 |
|
|
error being processed.</p> |
153 |
|
|
</dd> |
154 |
|
|
<dd> |
155 |
|
|
<p>Only one otherwise block may be specified per try block</p> |
156 |
|
|
</dd> |
157 |
|
|
<p></p> |
158 |
|
|
<dt><strong><a name="item_finally">finally BLOCK</a></strong><br /> |
159 |
|
|
</dt> |
160 |
|
|
<dd> |
161 |
|
|
Execute the code in <code>BLOCK</code> either after the code in the try block has |
162 |
|
|
successfully completed, or if the try block throws an error then |
163 |
|
|
<code>BLOCK</code> will be executed after the handler has completed. |
164 |
|
|
</dd> |
165 |
|
|
<dd> |
166 |
|
|
<p>If the handler throws an error then the error will be caught, the |
167 |
|
|
finally block will be executed and the error will be re-thrown.</p> |
168 |
|
|
</dd> |
169 |
|
|
<dd> |
170 |
|
|
<p>Only one finally block may be specified per try block</p> |
171 |
|
|
</dd> |
172 |
|
|
<p></p></dl> |
173 |
|
|
<p> |
174 |
|
|
</p> |
175 |
|
|
<hr /> |
176 |
|
|
<h1><a name="class_interface">CLASS INTERFACE</a></h1> |
177 |
|
|
<p> |
178 |
|
|
</p> |
179 |
|
|
<h2><a name="constructors">CONSTRUCTORS</a></h2> |
180 |
|
|
<p>The <code>Error</code> object is implemented as a HASH. This HASH is initialized |
181 |
|
|
with the arguments that are passed to it's constructor. The elements |
182 |
|
|
that are used by, or are retrievable by the <code>Error</code> class are listed |
183 |
|
|
below, other classes may add to these.</p> |
184 |
|
|
<pre> |
185 |
|
|
-file |
186 |
|
|
-line |
187 |
|
|
-text |
188 |
|
|
-value |
189 |
|
|
-object</pre> |
190 |
|
|
<p>If <code>-file</code> or <code>-line</code> are not specified in the constructor arguments |
191 |
|
|
then these will be initialized with the file name and line number where |
192 |
|
|
the constructor was called from.</p> |
193 |
|
|
<p>If the error is associated with an object then the object should be |
194 |
|
|
passed as the <code>-object</code> argument. This will allow the <code>Error</code> package |
195 |
|
|
to associate the error with the object.</p> |
196 |
|
|
<p>The <code>Error</code> package remembers the last error created, and also the |
197 |
|
|
last error associated with a package. This could either be the last |
198 |
|
|
error created by a sub in that package, or the last error which passed |
199 |
|
|
an object blessed into that package as the <code>-object</code> argument.</p> |
200 |
|
|
<dl> |
201 |
|
|
<dt><strong><a name="item_throw">throw ( [ ARGS ] )</a></strong><br /> |
202 |
|
|
</dt> |
203 |
|
|
<dd> |
204 |
|
|
Create a new <code>Error</code> object and throw an error, which will be caught |
205 |
|
|
by a surrounding <a href="#item_try"><code>try</code></a> block, if there is one. Otherwise it will cause |
206 |
|
|
the program to exit. |
207 |
|
|
</dd> |
208 |
|
|
<dd> |
209 |
|
|
<p><a href="#item_throw"><code>throw</code></a> may also be called on an existing error to re-throw it.</p> |
210 |
|
|
</dd> |
211 |
|
|
<p></p> |
212 |
|
|
<dt><strong><a name="item_with">with ( [ ARGS ] )</a></strong><br /> |
213 |
|
|
</dt> |
214 |
|
|
<dd> |
215 |
|
|
Create a new <code>Error</code> object and returns it. This is defined for |
216 |
|
|
syntactic sugar, eg |
217 |
|
|
</dd> |
218 |
|
|
<dd> |
219 |
|
|
<pre> |
220 |
|
|
die with Some::Error ( ... );</pre> |
221 |
|
|
</dd> |
222 |
|
|
<p></p> |
223 |
|
|
<dt><strong><a name="item_record">record ( [ ARGS ] )</a></strong><br /> |
224 |
|
|
</dt> |
225 |
|
|
<dd> |
226 |
|
|
Create a new <code>Error</code> object and returns it. This is defined for |
227 |
|
|
syntactic sugar, eg |
228 |
|
|
</dd> |
229 |
|
|
<dd> |
230 |
|
|
<pre> |
231 |
|
|
record Some::Error ( ... ) |
232 |
|
|
and return;</pre> |
233 |
|
|
</dd> |
234 |
|
|
<p></p></dl> |
235 |
|
|
<p> |
236 |
|
|
</p> |
237 |
|
|
<h2><a name="static_methods">STATIC METHODS</a></h2> |
238 |
|
|
<dl> |
239 |
|
|
<dt><strong><a name="item_prior">prior ( [ PACKAGE ] )</a></strong><br /> |
240 |
|
|
</dt> |
241 |
|
|
<dd> |
242 |
|
|
Return the last error created, or the last error associated with |
243 |
|
|
<code>PACKAGE</code> |
244 |
|
|
</dd> |
245 |
|
|
<p></p></dl> |
246 |
|
|
<p> |
247 |
|
|
</p> |
248 |
|
|
<h2><a name="object_methods">OBJECT METHODS</a></h2> |
249 |
|
|
<dl> |
250 |
|
|
<dt><strong><a name="item_stacktrace">stacktrace</a></strong><br /> |
251 |
|
|
</dt> |
252 |
|
|
<dd> |
253 |
|
|
If the variable <code>$Error::Debug</code> was non-zero when the error was |
254 |
|
|
created, then <a href="#item_stacktrace"><code>stacktrace</code></a> returns a string created by calling |
255 |
|
|
<code>Carp::longmess</code>. If the variable was zero the <a href="#item_stacktrace"><code>stacktrace</code></a> returns |
256 |
|
|
the text of the error appended with the filename and line number of |
257 |
|
|
where the error was created, providing the text does not end with a |
258 |
|
|
newline. |
259 |
|
|
</dd> |
260 |
|
|
<p></p> |
261 |
|
|
<dt><strong><a name="item_object">object</a></strong><br /> |
262 |
|
|
</dt> |
263 |
|
|
<dd> |
264 |
|
|
The object this error was associated with |
265 |
|
|
</dd> |
266 |
|
|
<p></p> |
267 |
|
|
<dt><strong><a name="item_file">file</a></strong><br /> |
268 |
|
|
</dt> |
269 |
|
|
<dd> |
270 |
|
|
The file where the constructor of this error was called from |
271 |
|
|
</dd> |
272 |
|
|
<p></p> |
273 |
|
|
<dt><strong><a name="item_line">line</a></strong><br /> |
274 |
|
|
</dt> |
275 |
|
|
<dd> |
276 |
|
|
The line where the constructor of this error was called from |
277 |
|
|
</dd> |
278 |
|
|
<p></p> |
279 |
|
|
<dt><strong><a name="item_text">text</a></strong><br /> |
280 |
|
|
</dt> |
281 |
|
|
<dd> |
282 |
|
|
The text of the error |
283 |
|
|
</dd> |
284 |
|
|
<p></p></dl> |
285 |
|
|
<p> |
286 |
|
|
</p> |
287 |
|
|
<h2><a name="overload_methods">OVERLOAD METHODS</a></h2> |
288 |
|
|
<dl> |
289 |
|
|
<dt><strong><a name="item_stringify">stringify</a></strong><br /> |
290 |
|
|
</dt> |
291 |
|
|
<dd> |
292 |
|
|
A method that converts the object into a string. This method may simply |
293 |
|
|
return the same as the <a href="#item_text"><code>text</code></a> method, or it may append more |
294 |
|
|
information. For example the file name and line number. |
295 |
|
|
</dd> |
296 |
|
|
<dd> |
297 |
|
|
<p>By default this method returns the <code>-text</code> argument that was passed to |
298 |
|
|
the constructor, or the string <code>"Died"</code> if none was given.</p> |
299 |
|
|
</dd> |
300 |
|
|
<p></p> |
301 |
|
|
<dt><strong><a name="item_value">value</a></strong><br /> |
302 |
|
|
</dt> |
303 |
|
|
<dd> |
304 |
|
|
A method that will return a value that can be associated with the |
305 |
|
|
error. For example if an error was created due to the failure of a |
306 |
|
|
system call, then this may return the numeric value of <code>$!</code> at the |
307 |
|
|
time. |
308 |
|
|
</dd> |
309 |
|
|
<dd> |
310 |
|
|
<p>By default this method returns the <code>-value</code> argument that was passed |
311 |
|
|
to the constructor.</p> |
312 |
|
|
</dd> |
313 |
|
|
<p></p></dl> |
314 |
|
|
<p> |
315 |
|
|
</p> |
316 |
|
|
<hr /> |
317 |
|
|
<h1><a name="predefined_error_classes">PRE-DEFINED ERROR CLASSES</a></h1> |
318 |
|
|
<dl> |
319 |
|
|
<dt><strong><a name="item_error_3a_3asimple">Error::Simple</a></strong><br /> |
320 |
|
|
</dt> |
321 |
|
|
<dd> |
322 |
|
|
This class can be used to hold simple error strings and values. It's |
323 |
|
|
constructor takes two arguments. The first is a text value, the second |
324 |
|
|
is a numeric value. These values are what will be returned by the |
325 |
|
|
overload methods. |
326 |
|
|
</dd> |
327 |
|
|
<dd> |
328 |
|
|
<p>If the text value ends with <code>at file line 1</code> as $@ strings do, then |
329 |
|
|
this infomation will be used to set the <code>-file</code> and <code>-line</code> arguments |
330 |
|
|
of the error object.</p> |
331 |
|
|
</dd> |
332 |
|
|
<dd> |
333 |
|
|
<p>This class is used internally if an eval'd block die's with an error |
334 |
|
|
that is a plain string.</p> |
335 |
|
|
</dd> |
336 |
|
|
<p></p></dl> |
337 |
|
|
<p> |
338 |
|
|
</p> |
339 |
|
|
<hr /> |
340 |
|
|
<h1><a name="known_bugs">KNOWN BUGS</a></h1> |
341 |
|
|
<p>None, but that does not mean there are not any.</p> |
342 |
|
|
<p> |
343 |
|
|
</p> |
344 |
|
|
<hr /> |
345 |
|
|
<h1><a name="authors">AUTHORS</a></h1> |
346 |
|
|
<p>Graham Barr <<a href="mailto:gbarr@pobox.com">gbarr@pobox.com</a>></p> |
347 |
|
|
<p>The code that inspired me to write this was originally written by |
348 |
|
|
Peter Seibel <<a href="mailto:peter@weblogic.com">peter@weblogic.com</a>> and adapted by Jesse Glick |
349 |
|
|
<<a href="mailto:jglick@sig.bsh.com">jglick@sig.bsh.com</a>>.</p> |
350 |
|
|
<p> |
351 |
|
|
</p> |
352 |
|
|
<hr /> |
353 |
|
|
<h1><a name="maintainer">MAINTAINER</a></h1> |
354 |
|
|
<p>Arun Kumar U <<a href="mailto:u_arunkumar@yahoo.com">u_arunkumar@yahoo.com</a>></p> |
355 |
|
|
|
356 |
|
|
</body> |
357 |
|
|
|
358 |
|
|
</html> |