File: tut_exceptions.html

package info (click to toggle)
rubybook 0.2.1-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 4,248 kB
  • ctags: 1,042
  • sloc: xml: 60,486; makefile: 25
file content (448 lines) | stat: -rw-r--r-- 20,016 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
       BODY    { margin-left: 1in;
                 width: 6in;
                 font-family: helvetica, arial, sans-serif;
               }
       H1      { color: #000080;
                 font-family: helvetica, arial, sans-serif;
                 font-size: 22pt;
                 margin-left: 0in
               }
       H2      { color: #000080;  font: bold x-large helvetica, sans-serif;
                 margin-left: 0in }
       H3      { color: #000080;  font: bold   large helvetica, sans-serif; }
       H4      { color: #000080;  font: italic large helvetica, sans-serif; }
       .ruby   { background: #fff0f0 }
       .header { color: white }
       .subheader { color: #ffdddd }
       .sidebar   { width: 6in }
       span.sans { font-family: helvetica, arial, sans-serif }
       -->
   </STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="tut_expressions.html">Previous &lt;</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="tut_modules.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
    Copyright (c) 2001 by Addison Wesley Longman.  This
    material may be distributed only subject to the terms and
    conditions set forth in the Open Publication License, v1.0 or
    later (the latest version is presently available at
    http://www.opencontent.org/openpub/).
-->
<h1>Exceptions, Catch, and Throw</h1><hr><br>
<P></P>
So far we're been developing code in Pleasantville, a wonderful
place where nothing ever, ever goes wrong.
Every library call
succeeds, users never enter incorrect data, and resources are
plentiful and cheap.  Well, that's about to change. Welcome to the
real world!
<P></P>
In the real world, errors happen. Good programs (and programmers)
anticipate them and arrange to handle them gracefully. This isn't
always as easy as it might be. Often the code that detects an error
does not have the context to know what to do about it. For example,
attempting to open a file that doesn't exist is acceptable in some
circumstances and is a fatal error at other times. What's your
file-handling module to do?
<P></P>
The traditional approach is to use return codes. The <code>open</code>
method returns some specific value to say it failed. This value
is then propagated back through the layers of calling routines
until someone wants to take responsibility for it.
<P></P>
The problem with this approach is that managing all these error codes
can be a pain. If a function calls <code>open</code>, then <code>read</code>,
and finally <code>close</code>, and each can return an error indication, how
can the function distinguish these error codes in the value it returns
to <em>its</em> caller?
<P></P>
To a large extent, exceptions solve this problem. Exceptions let you
package up information about an error into an object. That exception
object is then propagated back up the calling stack automatically
until the runtime system finds code that explicitly declares that it
knows how to handle that type of exception.
<h2>The Exception Class</h2>
<P></P>
The package that contains the information about an exception is an
object of class <code>Exception</code>, or one of class <code>Exception</code>'s
children. Ruby predefines a tidy hierarchy of exceptions, shown in
Figure 8.1 on page 93. As we'll see later, this hierarchy
makes handling exceptions considerably easier.
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>Figure not available...</td></tr></table>
<P></P>
When you need to raise an exception, you can use one of the built-in
<code>Exception</code> classes, or you can create one of your own. If you
create your own, you might want to make it a subclass of
<code>StandardError</code> or one of its children. If you don't, your exception
won't be caught by default.
<P></P>
Every <code>Exception</code> has associated with it a message string and a
stack backtrace. If you define your own exceptions, you can add
additional information.
<h2>Handling Exceptions</h2>
<P></P>
Our jukebox downloads songs from the Internet using a TCP socket. The
basic code is simple:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
opFile&nbsp;=&nbsp;File.open(opName,&nbsp;"w")
while&nbsp;data&nbsp;=&nbsp;socket.read(512)
&nbsp;&nbsp;opFile.write(data)
end
</pre></td></tr></table>

<P></P>
What happens if we get a fatal error halfway through the download? We
certainly don't want to store an incomplete song in the song list.
``I Did It My *click*''.
<P></P>
Let's add some exception handling code and see how it helps.
We
enclose the code that could raise an exception in a
<code>begin</code>/<code>end</code> block and use <code>rescue</code> clauses to tell Ruby the
types of exceptions we want to handle. In this case we're interested
in trapping <code>SystemCallError</code> exceptions (and, by implication, any
exceptions that are subclasses of <code>SystemCallError</code>), so that's what
appears on the <code>rescue</code> line.  In the error handling block, we
report the error, close and delete the output file, and then reraise
the exception.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
opFile&nbsp;=&nbsp;File.open(opName,&nbsp;"w")
begin
&nbsp;&nbsp;#&nbsp;Exceptions&nbsp;raised&nbsp;by&nbsp;this&nbsp;code&nbsp;will
&nbsp;&nbsp;#&nbsp;be&nbsp;caught&nbsp;by&nbsp;the&nbsp;following&nbsp;rescue&nbsp;clause
&nbsp;&nbsp;while&nbsp;data&nbsp;=&nbsp;socket.read(512)
&nbsp;&nbsp;&nbsp;&nbsp;opFile.write(data)
&nbsp;&nbsp;end
<P></P>
rescue&nbsp;SystemCallError
&nbsp;&nbsp;$stderr.print&nbsp;"IO&nbsp;failed:&nbsp;"&nbsp;+&nbsp;$!
&nbsp;&nbsp;opFile.close
&nbsp;&nbsp;File.delete(opName)
&nbsp;&nbsp;raise
end
</pre></td></tr></table>

<P></P>
When an exception is raised, and independent of any subsequent
exception handling, Ruby places a reference to the <code>Exception</code>
object associated with the exception in the global variable <code>$!</code>
(the exclamation point presumably mirroring our surprise that any of
<em>our</em> code could cause errors). In the previous example, we used
this variable to format our error message.
<P></P>
After closing and deleting the file, we call <code>raise</code> with no
parameters, which reraises the exception in <code>$!</code>. This is a
useful technique, as it allows you to write code that filters
exceptions, passing on those you can't handle to higher levels.  It's
almost like implementing an inheritance hierarchy for error
processing.
<P></P>
You can have multiple <code>rescue</code> clauses in a <code>begin</code> block, and
each <code>rescue</code> clause can specify multiple exceptions to catch.  At
the end of each rescue clause you can give Ruby the name of a local
variable to receive the matched exception. Many people find this more
readable than using <code>$!</code> all over the place.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
begin
&nbsp;&nbsp;eval&nbsp;string
rescue&nbsp;SyntaxError,&nbsp;NameError&nbsp;=>&nbsp;boom
&nbsp;&nbsp;print&nbsp;"String&nbsp;doesn't&nbsp;compile:&nbsp;"&nbsp;+&nbsp;boom
rescue&nbsp;StandardError&nbsp;=>&nbsp;bang
&nbsp;&nbsp;print&nbsp;"Error&nbsp;running&nbsp;script:&nbsp;"&nbsp;+&nbsp;bang
end
</pre></td></tr></table>

<P></P>
How does Ruby decide which rescue clause to execute? It turns out that
the processing is pretty similar to that used by the <code>case</code>
statement. For each <code>rescue</code> clause in the <code>begin</code> block, Ruby
compares the raised exception against each of the parameters in turn.
If the raised exception matches a parameter, Ruby executes the body of
the <code>rescue</code> and stops looking. The match is made using
<code>$!.kind_of?(<em>parameter</em>)</code>, and so will succeed if the parameter
has the same class as the exception or is an ancestor of the
exception.  If you write a <code>rescue</code> clause with no parameter list,
the parameter defaults to <code>StandardError</code>.
<P></P>
If no <code>rescue</code> clause matches, or if an exception is raised outside
a <code>begin</code>/<code>end</code> block, Ruby moves up the stack
and looks for an
exception handler in the caller, then in the caller's caller, and so on.
<P></P>
Although the parameters to the <code>rescue</code> clause are typically the
names of <code>Exception</code> classes, they can actually be arbitrary
expressions (including method calls) that return an <code>Exception</code> class.
<h3>Tidying Up</h3>
<P></P>
Sometimes you need to guarantee that some processing is done at the
end of a block of code, regardless of whether an exception was raised.
For example, you may have a file open on entry to the block, and you
need to make sure it gets closed as the block exits.
<P></P>
The <code>ensure</code> clause does just this.
<code>ensure</code> goes after the last 
<code>rescue</code> clause and contains a chunk of code that will always be
executed as the block terminates. It doesn't matter if the block exits 
normally, if it raises and rescues an exception, or if it is terminated
by an uncaught exception---the <code>ensure</code> block will get run.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
f&nbsp;=&nbsp;File.open("testfile")
begin
&nbsp;&nbsp;#&nbsp;..&nbsp;process
rescue
&nbsp;&nbsp;#&nbsp;..&nbsp;handle&nbsp;error
ensure
&nbsp;&nbsp;f.close&nbsp;unless&nbsp;f.nil?
end
</pre></td></tr></table>

<P></P>
The <code>else</code>
clause is a similar, although less useful, construct. If 
present, it goes after the <code>rescue</code> clauses and before any
<code>ensure</code>. The body of an <code>else</code> clause is executed only if no
exceptions are raised by the main body of code.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
f&nbsp;=&nbsp;File.open("testfile")
begin
&nbsp;&nbsp;#&nbsp;..&nbsp;process
rescue
&nbsp;&nbsp;#&nbsp;..&nbsp;handle&nbsp;error
else
&nbsp;&nbsp;puts&nbsp;"Congratulations--&nbsp;no&nbsp;errors!"
ensure
&nbsp;&nbsp;f.close&nbsp;unless&nbsp;f.nil?
end
</pre></td></tr></table>

<h3>Play It Again</h3>
<P></P>
Sometimes you may be able to correct the cause of an exception. In
those cases, you can use the <code>retry</code> statement within a <code>rescue</code>
clause to repeat the entire <code>begin</code>/<code>end</code> block.
Clearly there
is tremendous scope for infinite loops here, so this is a feature to
use with caution (and with a finger resting lightly on the interrupt
key).
<P></P>
As an example of code that retries on exceptions, have a look at the
following, adapted from Minero Aoki's <code>net/smtp.rb</code> library.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
@esmtp&nbsp;=&nbsp;true
<P></P>
begin
&nbsp;&nbsp;#&nbsp;First&nbsp;try&nbsp;an&nbsp;extended&nbsp;login.&nbsp;If&nbsp;it&nbsp;fails&nbsp;because&nbsp;the
&nbsp;&nbsp;#&nbsp;server&nbsp;doesn't&nbsp;support&nbsp;it,&nbsp;fall&nbsp;back&nbsp;to&nbsp;a&nbsp;normal&nbsp;login
<P></P>
&nbsp;&nbsp;if&nbsp;@esmtp&nbsp;then
&nbsp;&nbsp;&nbsp;&nbsp;@command.ehlo(helodom)
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;@command.helo(helodom)
&nbsp;&nbsp;end
<P></P>
rescue&nbsp;ProtocolError
&nbsp;&nbsp;if&nbsp;@esmtp&nbsp;then
&nbsp;&nbsp;&nbsp;&nbsp;@esmtp&nbsp;=&nbsp;false
&nbsp;&nbsp;&nbsp;&nbsp;retry
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;raise
&nbsp;&nbsp;end
end
</pre></td></tr></table>

<P></P>
This code tries first to connect to an SMTP server using the <code>EHLO</code> 
command, which is not universally supported. If the connection attempt
fails, the code sets the <code>@esmtp</code> variable to <code>false</code> and
retries the connection. If this fails again, the exception is reraised 
up to the caller.
<h2>Raising Exceptions</h2>
<P></P>
So far we've been on the defensive, handling exceptions raised by
others.
It's time to turn the tables and go on the offensive. (There
are those that say your gentle authors are always offensive, but
that's a different book.)
<P></P>
You can raise exceptions in your code with the <a href="ref_m_kernel.html#raise"><code>Kernel::raise</code></a>
method.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
raise
raise&nbsp;"bad&nbsp;mp3&nbsp;encoding"
raise&nbsp;InterfaceException,&nbsp;"Keyboard&nbsp;failure",&nbsp;caller
</pre></td></tr></table>

<P></P>
The first form simply reraises the current exception (or a
<code>RuntimeError</code> if there is no current exception). This is used in 
exception handlers that need to intercept an exception before passing
it on.
<P></P>
The second form creates a new <code>RuntimeError</code> exception, setting its 
message to the given string. This exception is then raised up the call 
stack.
<P></P>
The third form uses the first argument to create an exception and then
sets the associated message to the second argument and the stack
trace to the third argument. Typically the first argument will be either the
name of a class in the <code>Exception</code> hierarchy or a reference to an
object instance of one of these classes.<em>[Technically, this
  argument can be any object that responds to the message
  <code>exception</code> by returning an object such that
  <code>object.kind_of?(Exception)</code> is true.]</em> The stack trace is
normally produced using the <a href="ref_m_kernel.html#caller"><code>Kernel::caller</code></a> method.
<P></P>
Here are some typical examples of <code>raise</code> in action.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
raise
<P></P>
raise&nbsp;"Missing&nbsp;name"&nbsp;if&nbsp;name.nil?
<P></P>
if&nbsp;i&nbsp;>=&nbsp;myNames.size
&nbsp;&nbsp;raise&nbsp;IndexError,&nbsp;"#{i}&nbsp;>=&nbsp;size&nbsp;(#{myNames.size})"
end
<P></P>
raise&nbsp;ArgumentError,&nbsp;"Name&nbsp;too&nbsp;big",&nbsp;caller
</pre></td></tr></table>

<P></P>
In the last example, we remove the current routine from the stack
backtrace, which is often useful in library modules. We can take this
further: the following code removes two routines from the backtrace.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
raise&nbsp;ArgumentError,&nbsp;"Name&nbsp;too&nbsp;big",&nbsp;caller[1..-1]
</pre></td></tr></table>

<h3>Adding Information to Exceptions</h3>
<P></P>
You can define your own exceptions to hold any information that you
need to pass out from the site of an error. For example, certain types 
of network errors might be transient depending on the circumstances.
If such an error occurs, and the circumstances are right, you could
set a flag in the exception to tell the handler that it might be worth 
retrying the operation.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
class&nbsp;RetryException&nbsp;&lt;&nbsp;RuntimeError
&nbsp;&nbsp;attr&nbsp;:okToRetry
&nbsp;&nbsp;def&nbsp;initialize(okToRetry)
&nbsp;&nbsp;&nbsp;&nbsp;@okToRetry&nbsp;=&nbsp;okToRetry
&nbsp;&nbsp;end
end
</pre></td></tr></table>

<P></P>
Somewhere down in the depths of the code, a transient error occurs.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def&nbsp;readData(socket)
&nbsp;&nbsp;data&nbsp;=&nbsp;socket.read(512)
&nbsp;&nbsp;if&nbsp;data.nil?
&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;RetryException.new(true),&nbsp;"transient&nbsp;read&nbsp;error"
&nbsp;&nbsp;end
&nbsp;&nbsp;#&nbsp;..&nbsp;normal&nbsp;processing
end
</pre></td></tr></table>

<P></P>
Higher up the call stack, we handle the exception.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
begin
&nbsp;&nbsp;stuff&nbsp;=&nbsp;readData(socket)
&nbsp;&nbsp;#&nbsp;..&nbsp;process&nbsp;stuff
rescue&nbsp;RetryException&nbsp;=>&nbsp;detail
&nbsp;&nbsp;retry&nbsp;if&nbsp;detail.okToRetry
&nbsp;&nbsp;raise
end
</pre></td></tr></table>

<h2>Catch and Throw</h2>
<P></P>
While the exception mechanism of <code>raise</code> and <code>rescue</code> is great
for abandoning execution when things go wrong, it's sometimes nice to
be able to jump out of some deeply nested construct during normal
processing. This is where <code>catch</code> and <code>throw</code> come in handy.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
catch&nbsp;(:done)&nbsp;&nbsp;do
&nbsp;&nbsp;while&nbsp;gets
&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;:done&nbsp;unless&nbsp;fields&nbsp;=&nbsp;split(/\t/)
&nbsp;&nbsp;&nbsp;&nbsp;songList.add(Song.new(*fields))
&nbsp;&nbsp;end
&nbsp;&nbsp;songList.play
end
</pre></td></tr></table>

<P></P>
<code>catch</code> defines a block that is labeled with the given name
(which may be a <code>Symbol</code> or a <code>String</code>). The block is executed
normally until a <code>throw</code> is encountered.
<P></P>
When Ruby encounters a <code>throw</code>, it zips back up the call stack
looking for a <code>catch</code> block with a matching symbol.
When it finds
it, Ruby unwinds the stack to that point and terminates the block. If
the <code>throw</code> is called with the optional second parameter, that
value is returned as the value of the <code>catch</code>. So, in the previous
example, if the input does not contain correctly formatted lines, the
<code>throw</code> will skip to the end of the corresponding <code>catch</code>, not
only terminating the <code>while</code> loop but also skipping the playing of 
the song list.
<P></P>
The following example uses a <code>throw</code> to terminate interaction with
the user if ``!'' is typed in response to any prompt.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def&nbsp;promptAndGet(prompt)
&nbsp;&nbsp;print&nbsp;prompt
&nbsp;&nbsp;res&nbsp;=&nbsp;readline.chomp
&nbsp;&nbsp;throw&nbsp;:quitRequested&nbsp;if&nbsp;res&nbsp;==&nbsp;"!"
&nbsp;&nbsp;return&nbsp;res
end
<P></P>
catch&nbsp;:quitRequested&nbsp;do
&nbsp;&nbsp;name&nbsp;=&nbsp;promptAndGet("Name:&nbsp;")
&nbsp;&nbsp;age&nbsp;&nbsp;=&nbsp;promptAndGet("Age:&nbsp;&nbsp;")
&nbsp;&nbsp;sex&nbsp;&nbsp;=&nbsp;promptAndGet("Sex:&nbsp;&nbsp;")
&nbsp;&nbsp;#&nbsp;..
&nbsp;&nbsp;#&nbsp;process&nbsp;information
end
</pre></td></tr></table>

<P></P>
As this example illustrates, the <code>throw</code> does not have to appear within the
static scope of the <code>catch</code>.

<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="tut_expressions.html">Previous &lt;</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="tut_modules.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
     The Pragmatic Programmer's Guide"</font><br><font size="-3">
      Copyright
      &#169;
      2000 Addison Wesley Longman, Inc. Released under the terms of the
      <a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
        <br>
      This reference is available for
        <a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
   </font></body></html>