summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/html/k100080.html
blob: 85b751f5f768134dc5af1815ac9dabb6d8ed7ac4 (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100078.html;106,k100079.html;107,k100081.html;108,k100083.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100078.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100079.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100081.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100083.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">Kapitel 12 - Exceptions
</table>
<hr>


<!-- Section -->
<a name="sectlevel2id012002"></a>
<h2>12.2 Behandlung von Exceptions </h2>
<hr>
<ul>
<li><a href="k100080.html#sectlevel2id012002">12.2 Behandlung von Exceptions</a>
<ul>
<li><a href="k100080.html#sectlevel3id012002001">12.2.1 Die try-catch-Anweisung</a>
<li><a href="k100080.html#dasfehlerobjekt">12.2.2 Das Fehlerobjekt</a>
<li><a href="k100080.html#sectlevel3id012002003">12.2.3 Die Fehlerklassen von Java</a>
<li><a href="k100080.html#sectlevel3id012002004">12.2.4 Fortfahren nach Fehlern</a>
<li><a href="k100080.html#sectlevel3id012002005">12.2.5 Mehr als eine catch-Klausel</a>
<li><a href="k100080.html#sectlevel3id012002006">12.2.6 Die finally-Klausel</a>
</ul>
</ul>
<hr>


<!-- Section -->
<a name="sectlevel3id012002001"></a>
<h3>12.2.1 Die <a name="ixa100719">try-catch-Anweisung</a> </h3>

<p>
Das Behandeln von Ausnahmen erfolgt mit Hilfe der <a name="ixa100720"><a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a></a>-<a name="ixa100721"><a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a></a>-Anweisung:
<a name="listingid012001"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#0000AA">try</font> {
<font color="#555555">002 </font>  Anweisung;
<font color="#555555">003 </font>  ...
<font color="#555555">004 </font>} <font color="#0000AA">catch</font> (Ausnahmetyp x) {
<font color="#555555">005 </font>  Anweisung;
<font color="#555555">006 </font>  ...
<font color="#555555">007 </font>}</pre>
</font>
</td>
</tr>
</table>
<i>
Listing 12.1: Die try-catch-Anweisung</i></p>

<p>
Der <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Block
enth&auml;lt dabei eine oder mehrere Anweisungen, bei deren Ausf&uuml;hrung
ein Fehler des Typs <font color="#000077"><tt>Ausnahmetyp</tt></font>
auftreten kann. In diesem Fall wird die normale Programmausf&uuml;hrung
unterbrochen, und der Programmablauf f&auml;hrt mit der ersten Anweisung
nach der <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
fort, die den passenden Ausnahmetyp deklariert hat. Hier kann nun
Code untergebracht werden, der eine angemessene Reaktion auf den Fehler
realisiert. 

<p>
Wir wollen das folgende fehlerhafte Programm betrachten: 
<a name="listingid012002"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* RTErrorProg1.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> RTErrorProg1
<font color="#555555">004 </font>{
<font color="#555555">005 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font>  {
<font color="#555555">007 </font>    <font color="#006699">int</font> i, base = 0;
<font color="#555555">008 </font>
<font color="#555555">009 </font>    <font color="#0000AA">for</font> (base = 10; base &gt;= 2; --base) {
<font color="#555555">010 </font>      i = Integer.parseInt(<font color="#0000FF">"40"</font>,base);
<font color="#555555">011 </font>      System.out.println(<font color="#0000FF">"40 base "</font>+base+<font color="#0000FF">" = "</font>+i);
<font color="#555555">012 </font>    }
<font color="#555555">013 </font>  }
<font color="#555555">014 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/RTErrorProg1.java"><font color="#000055" size=-1>RTErrorProg1.java</font></a></td>
</tr>
</table>
<i>
Listing 12.2: Ein Programm mit einem Laufzeitfehler</i></p>

<p>
Hier soll der String &#187;40&#171; aus verschiedenen Zahlensystemen
in ein <a href="index_i.html#ixb100244"><font color=#000080><tt>int</tt></font></a>
konvertiert und als Dezimalzahl ausgegeben werden. Die dazu verwendete
Methode <a href="index_p.html#ixb100479"><font color=#000080><tt>parseInt</tt></font></a>
&uuml;berpr&uuml;ft, ob der &uuml;bergebene String einen g&uuml;ltigen
Zahlenwert zur angegebenen Basis darstellt. Ist dies nicht der Fall,
l&ouml;st sie eine Ausnahme des Typs <a name="ixa100722"><a href="index_n.html#ixb100571"><font color=#000080><tt>NumberFormatException</tt></font></a></a>
aus. Ohne weitere Ma&szlig;nahmen st&uuml;rzt das Programm dann beim
Versuch, den String &#187;40&#171; als Zahl zur Basis 4 anzusehen,
ab: 
<font color="#333300">
<pre>
40 base 10 = 40
40 base 9 = 36
40 base 8 = 32
40 base 7 = 28
40 base 6 = 24
40 base 5 = 20
Exception in thread "main" java.lang.NumberFormatException: 40
        at java.lang.Integer.parseInt(Compiled Code)
        at RTErrorProg1.main(Compiled Code)
</pre>
</font>

<p>
Das Programm l&auml;&szlig;t sich nun leicht gegen solche Fehler absichern,
indem die Programmsequenz, die den Fehler verursacht, in eine <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-<a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Anweisung
eingeschlossen wird: 
<a name="listingid012003"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1203.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1203
<font color="#555555">004 </font>{
<font color="#555555">005 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font>  {
<font color="#555555">007 </font>    <font color="#006699">int</font> i, base = 0;
<font color="#555555">008 </font>
<font color="#555555">009 </font>    <font color="#0000AA">try</font> {
<font color="#555555">010 </font>      <font color="#0000AA">for</font> (base = 10; base &gt;= 2; --base) {
<font color="#555555">011 </font>        i = Integer.parseInt(<font color="#0000FF">"40"</font>,base);
<font color="#555555">012 </font>        System.out.println(<font color="#0000FF">"40 base "</font>+base+<font color="#0000FF">" = "</font>+i);
<font color="#555555">013 </font>      }
<font color="#555555">014 </font>    } <font color="#0000AA">catch</font> (NumberFormatException e) {
<font color="#555555">015 </font>      System.out.println(
<font color="#555555">016 </font>        <font color="#0000FF">"40 ist keine Zahl zur Basis "</font>+base
<font color="#555555">017 </font>      );
<font color="#555555">018 </font>    }
<font color="#555555">019 </font>  }
<font color="#555555">020 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1203.java"><font color="#000055" size=-1>Listing1203.java</font></a></td>
</tr>
</table>
<i>
Listing 12.3: Abfangen des Laufzeitfehlers mit einer try-catch-Anweisung</i></p>

<p>
Zwar ist 40 immer noch keine Zahl zur Basis 4, aber das Programm f&auml;ngt
diesen Fehler nun ab und gibt eine geeignete Fehlermeldung aus: 
<font color="#333300">
<pre>
40 base 10 = 40
40 base 9 = 36
40 base 8 = 32
40 base 7 = 28
40 base 6 = 24
40 base 5 = 20
40 ist keine Zahl zur Basis 4
</pre>
</font>


<!-- Section -->
<a name="dasfehlerobjekt"></a>
<h3>12.2.2 Das <a name="ixa100723">Fehlerobjekt</a> </h3>

<p>
In der <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
wird nicht nur die Art des abzufangenden Fehlers definiert, sondern
auch ein formaler Parameter angegeben, der beim Auftreten der Ausnahme
ein Fehlerobjekt &uuml;bernehmen soll. Fehlerobjekte sind dabei Instanzen
der Klasse <a name="ixa100724"><a href="index_t.html#ixb100573"><font color=#000080><tt>Throwable</tt></font></a></a>
oder einer ihrer Unterklassen. Sie werden vom Aufrufer der Ausnahme
erzeugt und als Parameter an die <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
&uuml;bergeben. Das Fehlerobjekt enth&auml;lt Informationen &uuml;ber
die Art des aufgetretenen Fehlers. So liefert beispielsweise die Methode
<a name="ixa100725"><a href="index_g.html#ixb100574"><font color=#000080><tt>getMessage</tt></font></a></a>
einen Fehlertext (wenn dieser explizit gesetzt wurde), und <a name="ixa100726"><a href="index_p.html#ixb100575"><font color=#000080><tt>printStackTrace</tt></font></a></a>
druckt einen Auszug aus dem Laufzeit-Stack. Am einfachsten kann der
Fehlertext mit der Methode <a name="ixa100727"><a href="index_t.html#ixb100327"><font color=#000080><tt>toString</tt></font></a></a>
der Klasse <a href="index_t.html#ixb100573"><font color=#000080><tt>Throwable</tt></font></a>
ausgegeben werden: 
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public String getMessage()

public void printStackTrace()

public String toString()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/lang/Throwable.html" onClick="this.href=getApiDoc('java.lang.Throwable')"><font color="#660066" size=-1>java.lang.Throwable</font></a></td>
</tr>
</table>

<p>
Unser Beispielprogramm k&ouml;nnte dann wie folgt umgeschrieben werden:
<a name="listingid012004"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* RTErrorProg2.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> RTErrorProg2
<font color="#555555">004 </font>{
<font color="#555555">005 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font>  {
<font color="#555555">007 </font>    <font color="#006699">int</font> i, base = 0;
<font color="#555555">008 </font>
<font color="#555555">009 </font>    <font color="#0000AA">try</font> {
<font color="#555555">010 </font>      <font color="#0000AA">for</font> (base = 10; base &gt;= 2; --base) {
<font color="#555555">011 </font>        i = Integer.parseInt(<font color="#0000FF">"40"</font>,base);
<font color="#555555">012 </font>        System.out.println(<font color="#0000FF">"40 base "</font>+base+<font color="#0000FF">" = "</font>+i);
<font color="#555555">013 </font>      }
<font color="#555555">014 </font>    } <font color="#0000AA">catch</font> (NumberFormatException e) {
<font color="#555555">015 </font>      System.out.println(<font color="#0000FF">"***Fehler aufgetreten***"</font>);
<font color="#555555">016 </font>      System.out.println(<font color="#0000FF">"Ursache: "</font>+e.getMessage());
<font color="#555555">017 </font>      e.printStackTrace();
<font color="#555555">018 </font>    }
<font color="#555555">019 </font>  }
<font color="#555555">020 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/RTErrorProg2.java"><font color="#000055" size=-1>RTErrorProg2.java</font></a></td>
</tr>
</table>
<i>
Listing 12.4: Verwendung des Fehlerobjekts nach einem Laufzeitfehler</i></p>

<p>
Die Ausgabe des Programms w&auml;re dann: 
<font color="#333300">
<pre>
40 base 10 = 40
40 base 9 = 36
40 base 8 = 32
40 base 7 = 28
40 base 6 = 24
40 base 5 = 20
***Fehler aufgetreten***
Ursache: 40
java.lang.NumberFormatException: 40
        at java.lang.Integer.parseInt(Compiled Code)
        at RTErrorProg2.main(Compiled Code)
</pre>
</font>

<p>
Wie man sieht, &auml;hnelt die Ausgabe des Programms der ersten Version,
die ohne expliziten Fehler-Handler geschrieben wurde. Das liegt daran,
dass das Java-Laufzeitsystem beim Auftreten eines Fehlers, der von
keiner Methode behandelt wurde, <a name="ixa100726"><a href="index_p.html#ixb100575"><font color=#000080><tt>printStackTrace</tt></font></a></a>
aufruft, bevor es das Programm beendet. 

<!-- Section -->

<a name="sectlevel3id012002003"></a>
<h3>12.2.3 Die Fehlerklassen von Java </h3>

<p>
Alle Laufzeitfehler in Java sind Unterklassen der Klasse <a name="ixa100728"><a href="index_t.html#ixb100573"><font color=#000080><tt>Throwable</tt></font></a></a>.
<a href="index_t.html#ixb100573"><font color=#000080><tt>Throwable</tt></font></a>
ist eine allgemeine Fehlerklasse, die im wesentlichen eine Klartext-Fehlermeldung
speichern und einen Auszug des Laufzeit-Stacks ausgeben kann. Unterhalb
von <a href="index_t.html#ixb100573"><font color=#000080><tt>Throwable</tt></font></a>
befinden sich zwei gro&szlig;e Vererbungshierarchien: 
<ul>
<li>Die Klasse <a name="ixa100729"><a href="index_e.html#ixb100576"><font color=#000080><tt>Error</tt></font></a></a>
ist Superklasse aller <i>schwerwiegenden</i> Fehler. Diese werden
haupts&auml;chlich durch Probleme in der virtuellen Java-Maschine
ausgel&ouml;st. Fehler der Klasse <a href="index_e.html#ixb100576"><font color=#000080><tt>Error</tt></font></a>
sollten in der Regel nicht abgefangen werden, sondern (durch den Standard-Fehlerhandler)
nach einer entsprechenden Meldung zum Abbruch des Programms f&uuml;hren.
<li>Alle Fehler, die m&ouml;glicherweise f&uuml;r die Anwendung selbst
von Interesse sind, befinden sich in der Klasse <a name="ixa100730"><a href="index_e.html#ixb100577"><font color=#000080><tt>Exception</tt></font></a></a>
oder einer ihrer Unterklassen. Ein Fehler dieser Art signalisiert
einen abnormen Zustand, der vom Programm abgefangen und behandelt
werden kann.
</ul>

<p>
Viele Pakete der Java-Klassenbibliothek definieren ihre eigenen Fehlerklassen.
So gibt es spezielle Fehlerklassen f&uuml;r die Dateiein- und -ausgabe,
die Netzwerkkommunikation oder den Zugriff auf Arrays. Wir werden
diese speziellen Fehlerklassen immer dann erl&auml;utern, wenn eine
Methode besprochen wird, die diese Art von Fehler erzeugt. 

<!-- Section -->

<a name="sectlevel3id012002004"></a>
<h3>12.2.4 Fortfahren nach Fehlern </h3>

<p>
Die Reaktion auf eine Ausnahme muss keinesfalls zwangsl&auml;ufig
darin bestehen, das Programm zu beenden. Statt dessen kann auch versucht
werden, den Fehler zu beheben oder zu umgehen, um dann mit dem Programm
fortzufahren. Wird im obigen Programm die <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-<a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Anweisung
in die Schleife gesetzt, so f&auml;hrt das Programm nach jedem Fehler
fort und versucht, die Konvertierung zur n&auml;chsten Basis vorzunehmen:
<a name="listingid012005"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1205.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1205
<font color="#555555">004 </font>{
<font color="#555555">005 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font>  {
<font color="#555555">007 </font>    <font color="#006699">int</font> i, base = 0;
<font color="#555555">008 </font>
<font color="#555555">009 </font>    <font color="#0000AA">for</font> (base = 10; base &gt;= 2; --base) {
<font color="#555555">010 </font>      <font color="#0000AA">try</font> {
<font color="#555555">011 </font>        i = Integer.parseInt(<font color="#0000FF">"40"</font>,base);
<font color="#555555">012 </font>        System.out.println(<font color="#0000FF">"40 base "</font>+base+<font color="#0000FF">" = "</font>+i);
<font color="#555555">013 </font>      } <font color="#0000AA">catch</font> (NumberFormatException e) {
<font color="#555555">014 </font>        System.out.println(
<font color="#555555">015 </font>          <font color="#0000FF">"40 ist keine Zahl zur Basis "</font>+base
<font color="#555555">016 </font>        );
<font color="#555555">017 </font>      }
<font color="#555555">018 </font>    }
<font color="#555555">019 </font>  }
<font color="#555555">020 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1205.java"><font color="#000055" size=-1>Listing1205.java</font></a></td>
</tr>
</table>
<i>
Listing 12.5: Fortfahren nach Laufzeitfehlern</i></p>

<p>
Die Ausgabe des Programms lautet nun: 
<font color="#333300">
<pre>
40 base 10 = 40
40 base 9 = 36
40 base 8 = 32
40 base 7 = 28
40 base 6 = 24
40 base 5 = 20
40 ist keine Zahl zur Basis 4
40 ist keine Zahl zur Basis 3
40 ist keine Zahl zur Basis 2
</pre>
</font>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>

<p>
Ob es sinnvoller ist, nach einem Fehler mit der Programmausf&uuml;hrung
fortzufahren oder das Programm abzubrechen, ist von der Art des Fehlers
und der Stelle im Programm, an der er aufgetreten ist, abh&auml;ngig.
Hier muss von Fall zu Fall entschieden werden, wie vorgegangen werden
soll.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF">&nbsp;Hinweis&nbsp;</font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>


<!-- Section -->
<a name="sectlevel3id012002005"></a>
<h3>12.2.5 Mehr als eine catch-Klausel </h3>

<p>
Bisher sind wir davon ausgegangen, dass innerhalb eines <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Blocks
nur eine Ausnahme auftreten kann. Tats&auml;chlich ist es nat&uuml;rlich
ohne weiteres m&ouml;glich, dass zwei oder mehrere unterschiedliche
Ausnahmen ausgel&ouml;st werden. Das Programm kann auf verschiedene
Fehler reagieren, indem es mehr als eine <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
verwendet. Jede <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
f&auml;ngt die Fehler ab, die zum Typ des angegebenen Fehlerobjekts
zuweisungskompatibel sind. Dazu geh&ouml;ren alle Fehler der angegebenen
Klasse und all ihrer Unterklassen (das wichtige Konzept der Zuweisungskompatibilit&auml;t
von Objekttypen wurde in <a href="k100047.html#polymorphismus">Abschnitt 7.1.6</a>
erl&auml;utert). Die einzelnen <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klauseln
werden in der Reihenfolge ihres Auftretens abgearbeitet. 

<p>
Wir wollen das Beispielprogramm nun so erweitern, dass nicht nur ein
einzelner String in eine Zahl konvertiert wird, sondern ein Array
von Strings. Aufgrund eines Fehlers in der verwendeten <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
verursacht das Programm einen Zugriff auf ein nicht vorhandenes Array-Element
und l&ouml;st damit eine Ausnahme des Typs <a name="ixa100731"><a href="index_i.html#ixb100578"><font color=#000080><tt>IndexOutOfBoundsException</tt></font></a></a>
aus. Diese kann zusammen mit der Ausnahme <a href="index_n.html#ixb100571"><font color=#000080><tt>NumberFormatException</tt></font></a>
in einer gemeinsamen <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-<a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Anweisung
behandelt werden: 
<a name="listingid012006"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1206.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1206
<font color="#555555">004 </font>{
<font color="#555555">005 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font>  {
<font color="#555555">007 </font>    <font color="#006699">int</font> i, j, base = 0;
<font color="#555555">008 </font>    String[] numbers = <font color="#0000AA">new</font> String[3];
<font color="#555555">009 </font>
<font color="#555555">010 </font>    numbers[0] = <font color="#0000FF">"10"</font>;
<font color="#555555">011 </font>    numbers[1] = <font color="#0000FF">"20"</font>;
<font color="#555555">012 </font>    numbers[2] = <font color="#0000FF">"30"</font>;
<font color="#555555">013 </font>    <font color="#0000AA">try</font> {
<font color="#555555">014 </font>      <font color="#0000AA">for</font> (base = 10; base &gt;= 2; --base) {
<font color="#555555">015 </font>        <font color="#0000AA">for</font> (j = 0; j &lt;= 3; ++j) {
<font color="#555555">016 </font>          i = Integer.parseInt(numbers[j],base);
<font color="#555555">017 </font>          System.out.println(
<font color="#555555">018 </font>            numbers[j]+<font color="#0000FF">" base "</font>+base+<font color="#0000FF">" = "</font>+i
<font color="#555555">019 </font>          );
<font color="#555555">020 </font>        }
<font color="#555555">021 </font>      }
<font color="#555555">022 </font>    } <font color="#0000AA">catch</font> (IndexOutOfBoundsException e1) {
<font color="#555555">023 </font>      System.out.println(
<font color="#555555">024 </font>        <font color="#0000FF">"***IndexOutOfBoundsException: "</font> + e1.toString()
<font color="#555555">025 </font>      );
<font color="#555555">026 </font>    } <font color="#0000AA">catch</font> (NumberFormatException e2) {
<font color="#555555">027 </font>      System.out.println(
<font color="#555555">028 </font>        <font color="#0000FF">"***NumberFormatException: "</font> + e2.toString()
<font color="#555555">029 </font>      );
<font color="#555555">030 </font>    }
<font color="#555555">031 </font>  }
<font color="#555555">032 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1206.java"><font color="#000055" size=-1>Listing1206.java</font></a></td>
</tr>
</table>
<i>
Listing 12.6: Mehr als eine catch-Klausel</i></p>

<p>
Die Ausgabe des Programms ist nun: 
<font color="#333300">
<pre>
10 base 10 = 10
20 base 10 = 20
30 base 10 = 30
***IndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException
</pre>
</font>


<!-- Section -->
<a name="sectlevel3id012002006"></a>
<h3>12.2.6 Die finally-Klausel </h3>

<p>
Die <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-<a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Anweisung
enth&auml;lt einen optionalen Bestandteil, der bisher noch nicht erl&auml;utert
wurde. Mit Hilfe der <a name="ixa100732"><a href="index_f.html#ixb100579"><font color=#000080><tt>finally</tt></font></a></a>-Klausel,
die als letzter Bestandteil einer <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-<a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Anweisung
verwendet werden darf, kann ein Programmfragment definiert werden,
das immer dann ausgef&uuml;hrt wird, wenn die zugeh&ouml;rige <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Klausel
betreten wurde. Dabei spielt es keine Rolle, <i>welches</i> Ereignis
daf&uuml;r verantwortlich war, dass die <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Klausel
verlassen wurde. Die <a href="index_f.html#ixb100579"><font color=#000080><tt>finally</tt></font></a>-Klausel
wird insbesondere dann ausgef&uuml;hrt, wenn der <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Block
durch eine der folgenden Anweisungen verlassen wurde: 
<ul>
<li>wenn das normale Ende des <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Blocks
erreicht wurde
<li>wenn eine Ausnahme aufgetreten ist, die durch eine <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
behandelt wurde
<li>wenn eine Ausnahme aufgetreten ist, die nicht durch eine <a href="index_c.html#ixb100570"><font color=#000080><tt>catch</tt></font></a>-Klausel
behandelt wurde
<li>wenn der <a href="index_t.html#ixb100569"><font color=#000080><tt>try</tt></font></a>-Block
durch eine der Sprunganweisungen <a name="ixa100733"><a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a></a>,
<a name="ixa100734"><a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a></a>
oder <a name="ixa100735"><a href="index_r.html#ixb100333"><font color=#000080><tt>return</tt></font></a></a>
verlassen werden soll
</ul>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>

<p>
Die <a href="index_f.html#ixb100579"><font color=#000080><tt>finally</tt></font></a>-Klausel
ist also der ideale Ort, um Aufr&auml;umarbeiten durchzuf&uuml;hren.
Hier k&ouml;nnen beispielsweise Dateien geschlossen oder Ressourcen
freigegeben werden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF">&nbsp;Hinweis&nbsp;</font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>

<p>
Die folgende Variation unseres Beispielprogramms benutzt <a href="index_f.html#ixb100579"><font color=#000080><tt>finally</tt></font></a>
dazu, am Ende des Programms eine Meldung auszugeben: 
<a name="listingid012007"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1207.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1207
<font color="#555555">004 </font>{
<font color="#555555">005 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font>  {
<font color="#555555">007 </font>    <font color="#006699">int</font> i, base = 0;
<font color="#555555">008 </font>
<font color="#555555">009 </font>    <font color="#0000AA">try</font> {
<font color="#555555">010 </font>      <font color="#0000AA">for</font> (base = 10; base &gt;= 2; --base) {
<font color="#555555">011 </font>        i = Integer.parseInt(<font color="#0000FF">"40"</font>,base);
<font color="#555555">012 </font>        System.out.println(<font color="#0000FF">"40 base "</font>+base+<font color="#0000FF">" = "</font>+i);
<font color="#555555">013 </font>      }
<font color="#555555">014 </font>    } <font color="#0000AA">catch</font> (NumberFormatException e) {
<font color="#555555">015 </font>      System.out.println(
<font color="#555555">016 </font>        <font color="#0000FF">"40 ist keine Zahl zur Basis "</font>+base
<font color="#555555">017 </font>      );
<font color="#555555">018 </font>    } <font color="#0000AA">finally</font> {
<font color="#555555">019 </font>      System.out.println(
<font color="#555555">020 </font>        <font color="#0000FF">"Sie haben ein einfaches Beispiel "</font> +
<font color="#555555">021 </font>        <font color="#0000FF">"sehr gl&uuml;cklich gemacht."</font>
<font color="#555555">022 </font>      );
<font color="#555555">023 </font>    }
<font color="#555555">024 </font>  }
<font color="#555555">025 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1207.java"><font color="#000055" size=-1>Listing1207.java</font></a></td>
</tr>
</table>
<i>
Listing 12.7: Verwendung der finally-Klausel</i></p>

<p>
Die Ausgabe des Programms ist: 
<font color="#333300">
<pre>
40 base 10 = 40
40 base 9 = 36
40 base 8 = 32
40 base 7 = 28
40 base 6 = 24
40 base 5 = 20
40 ist keine Zahl zur Basis 4
Sie haben ein einfaches Beispiel sehr gl&uuml;cklich gemacht.
</pre>
</font>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100078.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100079.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100081.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100083.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">&copy; 1998, 2007 Guido Kr&uuml;ger &amp; Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>