Thinking in Java 3rd Edition
a0 1
a1 a2 2 a1
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
! " #$ %& ’ ( ) * +
#,-,’ ! " / 0 1 - 23 4 5 ’ %& ( " 6 ’ 7 8 9,;< = >? @ A #BC 0 3 4 DE F G H I JK 2L M N O P Q 7 8R23 4 %&
S T U 9 V W X Y < Z,- [ \ 7 8 @ ] #^ _ ‘ a 9,,bc d e f g #U h i j k X Y Z l m n #o p ^ \q r s tu e ^ \v w #x y z { | } @ ~ R\s tv #i S
3 4 + # #,0 1 #K 2 %&,< Z,,S #K? ¢ #£? ’ -,S #$ ¥?
,§ currency1 ', - 2 $? fi fla3a4 a5 a6#S k–. ;· § currency1
# –,@ n " #.?^ … ‰ § currency1? #K $ n` ′k %&,? ’ R2X Y? ^ 2? ˉ #?,^ ˙ ¨? RO ^ \¢R? 7
8 @ n b ˇ — %& ˇ @ ] # 7 8 U? -? a g #
R? m n? -Z 7 8? 1 V S
o ˉ #
=?Z 7 8 k # ;b c
f ) g? g ·
R\? - £ 3 4 0 " # ’ – L M? 7 8#+ –L M ’ ( · – ‘ # x ~- L M? 7 8# 7 8@ ] $
@ + 1 - ',#–? L M 3 4?#? n R\3 4,S – 3 4 g · #K ~- ‘? e
#^ ^
Z? - ^ \ Z ; 7 # B 7 8 - ' 8?
%& ; [ \ @ § currency1 ^ \ #i? 7 8 #1 #– @ n” "? § currency1
Z e ’ /? 7 834 $ ! ^ \/ "Z 7 8%&
@ R? m n’ –# $ # % & ;? # " ’ ( ‘ ) ) # * +?
,,%& #?Z,- A 7 8 - ' 8
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 2
a8 a9 3 a8
′k?Z 7 8,^ P A / 0 @ A #? !1 2 ;3 #? 4?Z 7 8#$ S + [ 5 ! 6 % 7 ~8 9? P Q 7 8?Z S T #? e,–?Z #
S ;@ n 3 4 "
Z ',
^? ’ < = P ( @ ntU > ^ > ’? @ ( · 3 4?Z ',A < 3 4 B’ ( ‘ #Rfl ; C A < 3 4 " #– F * ( D E - F G G H ‘ 7
8R\H I k?Z ',#′k – F G S 7 8R\3 4 GH #? ’ F * ( D E
@ ( · –5 ’ J F * ( D E #i? 3 4 g K ( D E R L?Z,0 ¢
Mn ^ \6 N L M ~MS O? #? · P,^ +
/ MQ O R? S T tU –L M – S V 3 4 g W # e? 7 8O? ’ X? + R ^ \Y Z ! s #? – L M? 7 8? #K L ^ \?Z # [ \ ]?
@ · F –L?Z " #- ^,¢ ~ ) 0 _ ;? b U 9
‘ K O #? b ^ \?Z ‘? a #
x y F * \]
R' \ ’? @ · # D?Z ‘ ! b F * ( D E F ! c · 1?Z 7 8 2 ( d| %& #9 ~· e ^ \fE @ ‘? @ R\%& R\ @?Z 7 8%&
#U g ’ D%& b 3 4 ! h i = ‘ #k %& tj k,· l ^ ' ( \ ]#tj? @ ( ·
m ^ \L?Z n o 6 N #
R\ ! 9 ’? - = pd #? – § currency1 ^ 9 ·,@ n – S? b ^ \ G H ‘ #i R\ ‘ L F * ( D E #RO ’
G H I q r K ( D E ! R/ " L ^ \?Z
R?
if(t == null)
throw new NullPointerException();
RO L?Z #k – F * ( D E R\3
4 x ~- ‘ 7 8 £,R 8 9 J–
Thinking in Java 3rd Edition
a7 3
a8 a9 3 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
! U 9 ‘ #–$ S a !? b?Z # ~” 9 s t u Q i 9 ’ v w x,- v y?Z,- z \s t uQ {^ \ | s t u Q #{} \ ;~ ^ \ F Q #?
– S?Z ! ^ 2 G H?
throw new NullPointerException("t = null");
–D #D‘ R\ S? @ n ‘
~C 0 U [ ¥ ¢ < Z? #–~ ‘? b ^ \ ',‘
x y R\ ‘ ! J
x R\ ‘ @ nu ;o p K? ‘ #+? a g #@n? o p R\ ‘ - ^? 8?Z 7 8 n \ # 9 ^? o p 2 + R n? # - –$
S L?Z @ nb @ n? ) ) #9 ~o p ^ \s #i? @ nt?
Z 7 8 b @ n! P Z o p fl = #? 9,o p
fl P Z @ n”,o p fl ‰ 9 ; >\?Z 7 8%& # R\%& ’?Z 0 @,
" [ K #–? S L
‘ R?Z < Z? #– V L?Z G H ′ x?Z ‘! G H #S T?Z k g K ( D E S R\?Z
‘ e N < Z #?Z —,^ G H #?Z ‘ ~ x -
@ nL?Z #K ;- ’ R\?Z #i? 7 8R\
Z %&?Z 7 8- ^ \ 7 # 9 ’ B– !? ¢ ^ \ @ 3 4 #x y D7 8 ’ ( ‘ £ ^ \ @;? 8?Z N O / #– _? ¥ B
§ R ^ currency1 ’ ~'?Z #i? y?,;7 8R2?Z
–b @ n L ^ \?Z
t R\@ n” £ ^ \@ n L ^ \?Z
#K L?Z #R\@ n~ ( –? /
@ n#K – S R\@ n w > b ^ \q r B?
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 4
a8 a9 4 a8
‘ R\?Z R / " B? #? – R
#? @ n” # B?,? y %&?
try {
// Code that might generate exceptions
}
–Z 7 8 %X Y fi § currency1 #K,
@ n " #–.,fl g P,',#4 i,^ \@ n " $ R m?Z 7 8 " #–,- –
# B? #x y ^ \ @,-?Z R a #‰7 8 ~ · ^ # `? +
F x #L ‘?Z ; @ 7 8 R\ @?
Z 7 8%& #–? ^ \?Z ;y? ^currency1?Z 7 8%&?Z 7 8%& ~˙ |,
# B? y #
try {
// Code that might generate exceptions
} catch(Type1 id1) {
// Handle exceptions of Type1
} catch(Type2 id2) {
// Handle exceptions of Type2
} catch(Type3 id3) {
// Handle exceptions of Type3
}
// etc..,
'
$ NZ 7 8%&,^ \” — #i? - ^ \ - ^ \q e — Q @ n?Z 7 8%& S @ n Q K O R2v …
‰ ‰ - #′k?Z —? J F G GH #? – R2v … #+ 9? K
Z 7 8%& ˙ |,
# B? y %& L?Z #?Z 78 2 ~–‘? e U Q?Z — v K 2?Z 7 8%& ! {^ \ x y – K '
$ N? #k 9?Z? 7 8 ^`
$ N? ′? #? e?Z 7 8%&? ~ v $N? ~ Rfl
X? #9? \ y? ^\
# y ˉ $ ~/
Thinking in Java 3rd Edition
a7 5
a8 a9 5 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
# # B? ! #- ’ ~˙,[ \ @ n~' ^Z?#+ R –5 ;^ \?Z 7 8%&
8* g D?Z 7 8¨ ’? z? — ! = —
#$
,? K e? #S? k –? n p 0 @ $ a #Rcurrency1 %& = #9? -?n ˇ p R\— #k 5 ’ L?Z #i? z { R\? p ‘
- ^? / "? @
9 #?Z 7 8%& ’? 2 S F * ( D E #x y k,g? K \@n#9? u {}? ’? g? @ #7 8‰?Z ) y #–
x z { ’? @ ( F * ˉ R #?Z,@n –?
!? # S R\? n‘ u ( D E
$? L?Z,^ \@ n‘ R\3 4 #? S
B? D #RO ~ i ( B? #˙ – ′?
S ‘ #%&, ˉ,? @ A ‘ 7 8?Z #
+,+ " #.~J (? @ A? ! = A,S? p g · #? @ A C ¢ #+? a g i K? ; 9, f
< Z?Z7 8%& L M?Z b a L #i ;Z L — R Z I +#$ n ˙ g ~ '
Z ˇ — 3 4 " #?
- o e –5 ’ 9 ;?Z % &?Z? ’ Y l,- –;/ 0?Z #,S – S –? b?Z ‘ – ’ ~C 3 4; b –?Z #– -?Z # g –;?
b K \?Z K \ x Z ’ m R^ fl? b ^\?Z n o m n ˙ | B ! 1 –? ^ \| s t uQ #RO m ^ ;+?
//,c09:SimpleExceptionDemo.java
// Inheriting your own exceptions,
import com.bruceeckel.simpletest.*;
class SimpleException extends Exception {}
public class SimpleExceptionDemo {
private static Test monitor = new Test();
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 6
a8 a9 6 a8
public void f() throws SimpleException {
System.out.println("Throw SimpleException from
f()");
throw new SimpleException();
}
public static void main(String[] args) {
SimpleExceptionDemo sed = new
SimpleExceptionDemo();
try {
sed.f();
} catch(SimpleException e) {
System.err.println("Caught it!");
}
monitor.expect(new String[] {
"Throw SimpleException from f()",
"Caught it!"
});
}
} ///:~
! 1? b ^ \| s t u Q #9 ~
i? ^? – @ A
,| s t u Q F x # R\6 N !
s t u Q - #+? a g 9 $ [ P? –D #?Z ‘ a # ; R\ #,S?ˇ [ Q " R\?Z $ G
R #~
′ 2 v y?
k G H ‘ a # R < Z ~ ; #? y j ’ ~/ e –D?
#K 4
/ e " #9? ~/ g #RO ’ C
–$ S –? b?Z e,S
Q s t u Q
//,c09:FullConstructors.java
import com.bruceeckel.simpletest.*;
class MyException extends Exception {
public MyException() {}
public MyException(String msg) { super(msg); }
}
public class FullConstructors {
private static Test monitor = new Test();
public static void f() throws MyException {
System.out.println("Throwing MyException from
f()");
throw new MyException();
}
public static void g() throws MyException {
System.out.println("Throwing MyException from
g()");
throw new MyException("Originated in g()");
Thinking in Java 3rd Edition
a7 7
a8 a9 7 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
}
public static void main(String[] args) {
try {
f();
} catch(MyException e) {
e.printStackTrace();
}
try {
g();
} catch(MyException e) {
e.printStackTrace();
}
monitor.expect(new String[] {
"Throwing MyException from f()",
"MyException",
"%% \tat FullConstructors.f\\(.*\\)",
"%% \tat FullConstructors.main\\(.*\\)",
"Throwing MyException from g()",
"MyException,Originated in g()",
"%% \tat FullConstructors.g\\(.*\\)",
"%% \tat FullConstructors.main\\(.*\\)"
});
}
} ///:~
$?
- z \s t u Q {} \< =? # Q ^ \
Q,s t u Q 7 8%&,
b 9 K?
@ n R\@ n~o p /,@ n = NO ^ \[ &?Z 0 fl G H #? #R2G H ~ vy? #+ R\@ n $ U –D′? U 9?
b e,?Z? ’ –^ –? S – s t u Q
//,c09:ExtraFeatures.java
// Further embellishment of exception classes,
import com.bruceeckel.simpletest.*;
class MyException2 extends Exception {
private int x;
public MyException2() {}
public MyException2(String msg) { super(msg); }
public MyException2(String msg,int x) {
super(msg);
this.x = x;
}
public int val() { return x; }
public String getMessage() {
return "Detail Message,"+ x + " "+
super.getMessage();
}
}
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 8
a8 a9 8 a8
public class ExtraFeatures {
private static Test monitor = new Test();
public static void f() throws MyException2 {
System.out.println("Throwing MyException2 from
f()");
throw new MyException2();
}
public static void g() throws MyException2 {
System.out.println("Throwing MyException2 from
g()");
throw new MyException2("Originated in g()");
}
public static void h() throws MyException2 {
System.out.println("Throwing MyException2 from
h()");
throw new MyException2("Originated in h()",47);
}
public static void main(String[] args) {
try {
f();
} catch(MyException2 e) {
e.printStackTrace();
}
try {
g();
} catch(MyException2 e) {
e.printStackTrace();
}
try {
h();
} catch(MyException2 e) {
e.printStackTrace();
System.err.println("e.val() = " + e.val());
}
monitor.expect(new String[] {
"Throwing MyException2 from f()",
"MyException2,Detail Message,0 null",
"%% \tat ExtraFeatures.f\\(.*\\)",
"%% \tat ExtraFeatures.main\\(.*\\)",
"Throwing MyException2 from g()",
"MyException2,Detail Message,0 Originated in
g()",
"%% \tat ExtraFeatures.g\\(.*\\)",
"%% \tat ExtraFeatures.main\\(.*\\)",
"Throwing MyException2 from h()",
"MyException2,Detail Message,47 Originated
in h()",
"%% \tat ExtraFeatures.h\\(.*\\)",
"%% \tat ExtraFeatures.main\\(.*\\)",
"e.val() = 47"
});
}
} ///:~
R\?Z? ^ \Q V?
#^ \’ ` R\Q V @ n?’ u e R\Q V s t u Q ’ ' ^ 2 - G H #9? +
Thinking in Java 3rd Edition
a7 9
a8 a9 9 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
@ n k?Z ‘ a #
- fl ′k?Z U 9 ‘ z O #,S – S R? @ n‘? R
e,?Z + ; #F – R2 " ’ J? %& " #,’ ~ R2 J #?,’ 5 o e R\L?Z #U 9,
ˇ [ Q !?Z,R
–0 K 2” –@ n? %&,#–e,@ n ’ ~L aZ R ^? m n#? R ^ ‘ #” @ n ’ L M + O ‘?Z F x # S
#? %& S < = currency1 e X? ‘ 0 1 Rfl#+ < Z? #,R#
X ng ;
$ 2 – R? X n #’ B– 0? %&,#R\@ n’ L?Z #RO? %& ’ 7 8 R?Z a
#9 j k @ n ^ > ’ #; Q? y
Z a ;
#y,g,- ’ L?Z — o,S @ n e,’ ~ RO?
void f() throws TooBig,TooSmall,DivZero { //..,

void f() { //,.,
R? R\@ n ~L?Z
M?
Z #R?Z ;?Z a S b @ L R^ fl£,S y ~%
– ’?Z a – @ n' ^ \?Z + R? 7 8#K
! 1 ~ –#; 7 8R\?Z #;?Z a > ’ ˉ R\@n~L R\?Z < = R? b K K?Z a #; ^ e % ! P?Z
=? - \’ @?– S " @ n~L ^ \? a g i ~L
Z ! 1 ~F ! #9 ~;3? %& ! ~L?Z K O
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 10
a8 a9 10 a8
R\@ n R? 7 #9 ’?Z " \? N #S y – ’! L?Z · #? -? k U? 1 ~! L?Z
! ‘ a #R F ; ~ ! – § currency1 i? 2 7 8?Z / "; b ^ \ ’ 7 8 —?Z?Z 7 8%& ‰ ’;m Rfl#– ·?Z? - ^ 2U 9 — #+? a g 5 -
%$?
catch(Exception e) {
System.err.println("Caught an exception");
}
RO ’,-?Z #,S –? R +? # 9
y #RO 9 ~% U 9 7 8%& *?Z J& ′k
5 K 2,%& -?Z #? – ’ b9 K [ k?Z G H # = –$ S,
@ n?
" # G H #tj F X Y G H
o p a w #R\
n ’ a -?
$
$ %
$ & $ % ’?
S T,( ) "
,? D–*?Z 0 @ n” [ & {^ \ ~? v y g #{}?{+ \ ~? –,-?
{ ( 7 #– ~L M ;- Rz
! (
‘,/ " ! ) F * 0 ‰ %& ; L t?Z " ; R2G H
= ^ ~? %
Thinking in Java 3rd Edition
a7 11
a8 a9 11 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
–? S #$ ) &,- ‘ @ n?Z ‘ a - ^ \ - @ n#
* 9 ~o p ^ \ R\ ‘ j k a? —
* ‘ | ‘ #– S
+ currency1 1 R\* ‘ –? S * ‘? i 2 ¢ # =?Z 7 8‘ a # R\ ;
Rcurrency1 %& 3? R2
@ n n?
//,c09:ExceptionMethods.java
// Demonstrating the Exception Methods,
import com.bruceeckel.simpletest.*;
public class ExceptionMethods {
private static Test monitor = new Test();
public static void main(String[] args) {
try {
throw new Exception("My Exception");
} catch(Exception e) {
System.err.println("Caught Exception");
System.err.println("getMessage():" +
e.getMessage());
System.err.println("getLocalizedMessage():" +
e.getLocalizedMessage());
System.err.println("toString():" + e);
System.err.println("printStackTrace():");
e.printStackTrace();
}
monitor.expect(new String[] {
"Caught Exception",
"getMessage():My Exception",
"getLocalizedMessage():My Exception",
"toString():java.lang.Exception,My Exception",
"printStackTrace():",
"java.lang.Exception,My Exception",
"%% \tat ExceptionMethods.main\\(.*\\)"
});
}
} ///:~
–’ R2@ n^ \ ^ \ ; [ G H? a g 9,? \,*
^ \ 4
- – ; L K \5 #
Z ′k –? - F *?Z
! #? – S ˙ | DK \ ! L ‘?
catch(Exception e) {
System.err.println("An exception was thrown");
throw e;
}
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 12
a8 a9 12 a8
L ~?Z r ^ K?Z 7 8%& · ^ \
B? U 9
N?,D/ 6 7 #9? ~ 8?Z ‘,- G H #RO R\?Z g ^ K?Z 7 8%& ’ G R\ ‘ !,- GH
–˙ | L F *?Z #
,? ‘ K2 x?Z ‘ G H #? ~ˉ?Z 0 @ #9,~/ ˉ – L?Z fl –;9 ( ) G H #– S,
! ( #R\@ n~DF * G H,–;?Z ‘! #i o p ^ \
‘ 9 <? @ A?
//,c09:Rethrowing.java
// Demonstrating fillInStackTrace()
import com.bruceeckel.simpletest.*;
public class Rethrowing {
private static Test monitor = new Test();
public static void f() throws Exception {
System.out.println("originating the exception in
f()");
throw new Exception("thrown from f()");
}
public static void g() throws Throwable {
try {
f();
} catch(Exception e) {
System.err.println("Inside
g(),e.printStackTrace()");
e.printStackTrace();
throw e; // 17
// throw e.fillInStackTrace(); // 18
}
}
public static void
main(String[] args) throws Throwable {
try {
g();
} catch(Exception e) {
System.err.println(
"Caught in main,e.printStackTrace()");
e.printStackTrace();
}
monitor.expect(new String[] {
"originating the exception in f()",
"Inside g(),e.printStackTrace()",
"java.lang.Exception,thrown from f()",
"%% \tat Rethrowing.f(.*?)",
"%% \tat Rethrowing.g(.*?)",
"%% \tat Rethrowing.main(.*?)",
"Caught in main,e.printStackTrace()",
"java.lang.Exception,thrown from f()",
"%% \tat Rethrowing.f(.*?)",
"%% \tat Rethrowing.g(.*?)",
"%% \tat Rethrowing.main(.*?)"
Thinking in Java 3rd Edition
a7 13
a8 a9 13 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
});
}
} ///:~; ^,? v ‘ {
* -?= $ 1?
#K * R\?Z / L [ $? #?Z ( ),~^ ˙ 9 b p K \L
{
*?= #i? { +?= p ‘ #K ~
! ( #′
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception,thrown from f()
at Rethrowing.f(Rethrowing.java:9)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:23)
Caught in main,e.printStackTrace()
java.lang.Exception,thrown from f()
at Rethrowing.g(Rethrowing.java:18)
at Rethrowing.main(Rethrowing.java:23)
- ^ 2? G H ′k
! ( #{ +Z 0 @ n
Z a ! #R?
! ( @ no p ‘ ! ′k
#,S - ’ ~ ^ \
#? 7 8%& - ’ ~ R\ L ‘ ‘ Q ^ >,’P Z ( # ! 1 ~ 2 ;3?Z a !
6? # Rcurrency1 %& !
’ L ‘?Z?
//,c09:ThrowOut.java
// {ThrowsException}
public class ThrowOut {
public static void
main(String[] args) throws Throwable {
try {
throw new Throwable();
} catch(Exception e) {
System.err.println("Caught in main()");
}
}
} ///:~
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 14
a8 a9 14 a8
–$ S L ^ \ –?Z?Z R m?
! (? [?Z p a 0 G H / #1 x L?Z fl
//,c09:RethrowNew.java
// Rethrow a different object from the one that was
caught,
// {ThrowsException}
import com.bruceeckel.simpletest.*;
class OneException extends Exception {
public OneException(String s) { super(s); }
}
class TwoException extends Exception {
public TwoException(String s) { super(s); }
}
public class RethrowNew {
private static Test monitor = new Test();
public static void f() throws OneException {
System.out.println("originating the exception in
f()");
throw new OneException("thrown from f()");
}
public static void
main(String[] args) throws TwoException {
try {
f();
} catch(OneException e) {
System.err.println(
"Caught in main,e.printStackTrace()");
e.printStackTrace();
throw new TwoException("from main()");
}
monitor.expect(new String[] {
"originating the exception in f()",
"Caught in main,e.printStackTrace()",
"OneException,thrown from f()",
"\tat RethrowNew.f(RethrowNew.java:18)",
"\tat RethrowNew.main(RethrowNew.java:22)",
"Exception in thread \"main\" " +
"TwoException,from main()",
"\tat RethrowNew.main(RethrowNew.java:28)"
});
}
} ///:~
y K \?Z 5 L M 9 b
! ‘ –@ $ ; 8* ^ \?Z t U 9?Z A 9,,
a? b #? B C } 1 ~ 9,8 D
Thinking in Java 3rd Edition
a7 15
a8 a9 15 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
– Z ~˙ ^ \?Z i? L £ ^ \?Z " #? ; x *^ \?Z G H? R,E?Z F
% &,-S * #%&,+ ‘ x * ^ \?Z G H #+ 1,-
N,- ^ \’ | G
‘ s t u Q R\ ‘ x * ^ \?Z #RO < = ^ ^ I q #4 –? b i L?Z #9 x ’? ^ \ ’? H?Z {^ 1 I ( )
- J #
N ! #5 - +Z ; *
Q s t u Q #9,;,/ / 0? ˉ ) #
–;F | U 9?Z #K ’ s t u Q # 5 ’
* @ n Rcurrency1 %& ’ B– (
,- ‘? QV?
//,c09:DynamicFields.java
// A Class that dynamically adds fields to itself,
// Demonstrates exception chaining,
// {ThrowsException}
import com.bruceeckel.simpletest.*;
class DynamicFieldsException extends Exception {}
public class DynamicFields {
private static Test monitor = new Test();
private Object[][] fields;
public DynamicFields(int initialSize) {
fields = new Object[initialSize][2];
for(int i = 0; i < initialSize; i++)
fields[i] = new Object[] { null,null };
}
public String toString() {
StringBuffer result = new StringBuffer();
for(int i = 0; i < fields.length; i++) {
result.append(fields[i][0]);
result.append(",");
result.append(fields[i][1]);
result.append("\n");
}
return result.toString();
}
private int hasField(String id) {
for(int i = 0; i < fields.length; i++)
if(id.equals(fields[i][0]))
return i;
return -1;
}
private int
getFieldNumber(String id) throws
NoSuchFieldException {
int fieldNum = hasField(id);
if(fieldNum == -1)
throw new NoSuchFieldException();
return fieldNum;
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 16
a8 a9 16 a8
}
private int makeField(String id) {
for(int i = 0; i < fields.length; i++)
if(fields[i][0] == null) {
fields[i][0] = id;
return i;
}
// No empty fields,Add one,
Object[][]tmp = new Object[fields.length + 1][2];
for(int i = 0; i < fields.length; i++)
tmp[i] = fields[i];
for(int i = fields.length; i < tmp.length; i++)
tmp[i] = new Object[] { null,null };
fields = tmp;
// Reursive call with expanded fields,
return makeField(id);
}
public Object
getField(String id) throws NoSuchFieldException {
return fields[getFieldNumber(id)][1];
}
public Object setField(String id,Object value)
throws DynamicFieldsException {
if(value == null) {
// Most exceptions don't have a "cause"
constructor,
// In these cases you must use initCause(),
// available in all Throwable subclasses,
DynamicFieldsException dfe =
new DynamicFieldsException();
dfe.initCause(new NullPointerException());
throw dfe;
}
int fieldNumber = hasField(id);
if(fieldNumber == -1)
fieldNumber = makeField(id);
Object result = null;
try {
result = getField(id); // Get old value
} catch(NoSuchFieldException e) {
// Use constructor that takes "cause",
throw new RuntimeException(e);
}
fields[fieldNumber][1] = value;
return result;
}
public static void main(String[] args) {
DynamicFields df = new DynamicFields(3);
System.out.println(df);
try {
df.setField("d","A value for d");
df.setField("number",new Integer(47));
df.setField("number2",new Integer(48));
System.out.println(df);
df.setField("d","A new value for d");
df.setField("number3",new Integer(11));
System.out.println(df);
System.out.println(df.getField("d"));
Object field = df.getField("a3"); // Exception
} catch(NoSuchFieldException e) {
Thinking in Java 3rd Edition
a7 17
a8 a9 17 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
throw new RuntimeException(e);
} catch(DynamicFieldsException e) {
throw new RuntimeException(e);
}
monitor.expect(new String[] {
"null,null",
"null,null",
"null,null",
"",
"d,A value for d",
"number,47",
"number2,48",
"",
"d,A new value for d",
"number,47",
"number2,48",
"number3,11",
"",
"A value for d",
"Exception in thread \"main\" " +
"java.lang.RuntimeException," +
"java.lang.NoSuchFieldException",
"\tat
DynamicFields.main(DynamicFields.java:98)",
"Caused by,java.lang.NoSuchFieldException",
"\tat DynamicFields.getFieldNumber(" +
"DynamicFields.java:37)",
"\tat
DynamicFields.getField(DynamicFields.java:58)",
"\tat
DynamicFields.main(DynamicFields.java:96)"
});
}
} ///:~
\
,- ‘,- ^ \ ) &,) & Q K {^ \ ‘? Q V v …
^ \ # {} \ R\? Q V s #R\s S M
) ) F –? b R\ ‘ " #–;L ^ 9 ˇ? ~- [ $? F –”
- " #9 ; V – ; u K \Q V #;? b ^ \? i –u s –· M ‰ #9 ~RO N O M b
^ \? K \Q K [ ^ \P Q Q K #x y? K \Q K ! PQ > R S – Q K –,T ^ \
#K 9 ~? b i L ^ \
,- #x y * D
+ % F? 0 U –·
-? ~” - i o p? Q V s #
- - ’ ~L ^ \+ -%&,
- #K V W 7 8
+ - #+ R\?Z b - L
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 18
a8 a9 18 a8
#K R ^ \ % #,S 9 ~ ~ 0 F Q s t u QD
+ - X?
,- ’ / FZ L ‘;’? z ’? z b 9 K? z \
! ˉ #R2,;–· Y M q r?
,- v y @ n – e,@ n S T %& ( 0 ’ L?Z?
%&,Z [ J?;Z - \ˇ? #? n \ ]
1 2 / 3 W ^ ^ …? s #RO ~Z - \Z … #+? Q [ – ~0 1 #?Z?Z ) M ) #
!?Z Q _ x? ) ! 5 ‘ R2?Z $ a,? –~ {+ @ #ˇ? $ - 9 ^ b?Z?Z ‘ a #; 8 9? § #S T N O (?Z?Z ‘ 3 4 #,S?Z R,[ $
S =?Z i,& $ e,- 2?Z U 9 #
# #? #c? R2?Z S b 9," t U d e?,-
4?Z,?
& $ ( )
7 {^ \6 N
if(t == null)
throw new NullPointerException();
– § currency1? ^ \I J@ n
! # 9 ′k – L M,@ n K ^ @ I J– ^ \-
! #K ! - flf g ( #– R m
~ 9 v y ( § currency1? ˉ –h e R 3 4 # -
! –,#K
~ L ^ \+ %,S g &? a g [ i
[Z 9,,′ L #? –?Z a ! 9,‘ 9,,?
R\ #? ‘ @ #? R$ ^\? (? 6 N 9? b ^ j - q k?
Thinking in Java 3rd Edition
a7 19
a8 a9 19 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
#′k 9,,#? –$?Z a !
tjZ
′k 9, 0 ’ #? < Z? #– ·
9 ~ 7 8 ;B–· § currency1
#K ~? l x –^ _ ~·
#+ F –? b " ’ " # ’? ~L ^ 2
– R?Z " R 0 2 S T ′k -?Z a # ! 1
$ 2 § currency1 #,S ~ G< m ^ \ n
R,0 2 #K,,Rcurrency1%&?
//,c09:NeverCaught.java
// Ignoring RuntimeExceptions,
// {ThrowsException}
import com.bruceeckel.simpletest.*;
public class NeverCaught {
private static Test monitor = new Test();
static void f() {
throw new RuntimeException("From f()");
}
static void g() {
f();
}
public static void main(String[] args) {
g();
monitor.expect(new String[] {
"Exception in thread \"main\" " +
"java.lang.RuntimeException,From f()",
" at NeverCaught.f(NeverCaught.java:7)",
" at
NeverCaught.g(NeverCaught.java:10)",
" at
NeverCaught.main(NeverCaught.java:13)"
});
}
} ///:~
–$
t ′9 o,^ 2q 6 #? ! 1 ~;3 – R2 —?Z? a
,S p ]
G< q r #9 ~ %& " #”
#′k % " # ! 1 ~ 2 ;3 –+,- U 9 7 8%& #
,S –5 ’ 6 7 T U N
%?
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 20
a8 a9 20 a8
1,^? – nY Z? – 2 ) w !
2,^? ′k %& s § currency1 9 § currency1 ',t? (?
/ ( ) ) ! 0 #–t 3 Q K " Q K ˇ u m ^ \§ currency1
{^ 0?Z # Z ~3 {} 3 4
–~ R? 7 8?Z 7 #,,= %! #9 ’ v g ˇ w
s x –? #– ’
Z 7 8F? ^? g ’ o ^ < #9 u p y ‘ 7 8K 2 – 2 fl w? Q,t? # Z ( #+ K 2 ! 1 n0 z %
#9 $ Z ;
–~ Z ˙ ^ 2 *
B? - - L?Z #%&,^ _ ‘ a R2,– h i w x )?
h i w x B C p } 1 $; R\? #– S?Z 7 8%& ) y
! N? a10a11a13a12 a14 k?Z 7 8 ‰? n?
try {
// The guarded region,Dangerous activities
// that might throw A,B,or C
} catch(A a1) {
// Handler for situation A
} catch(B b1) {
// Handler for situation B
} catch(C c1) {
// Handler for situation C
} finally {
// Activities that happen every time
};? {
! N? ) ~ #x ( Rcurrency1 %&?
//,c09:FinallyWorks.java
// The finally clause is always executed,
import com.bruceeckel.simpletest.*;
class ThreeException extends Exception {}
public class FinallyWorks {
private static Test monitor = new Test();
static int count = 0;
public static void main(String[] args) {
while(true) {
try {
// Post-increment is zero first time,
if(count++ == 0)
Thinking in Java 3rd Edition
a7 21
a8 a9 21 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
throw new ThreeException();
System.out.println("No exception");
} catch(ThreeException e) {
System.err.println("ThreeException");
} finally {
System.err.println("In finally clause");
if(count == 2) break; // out of "while"
}
}
monitor.expect(new String[] {
"ThreeException",
"In finally clause",
"No exception",
"In finally clause"
});
}
} ///:~
– S b %&? # * - - L?Z #
! N? ) ~
R\%& $ | 0 £,? 7 8£,* % = #
Z?
Z ^ O ~< = %& p?Z L @ h i #R\3 4 –
B? –^ \ D #– ’ s b ^ \%& ( ) * F ',–$ S D? g
Q 1 #tU 9 #B9 ) * [,^? @ n RO – ’ %& ’ g ^ \ }
- B C } 1 ~,~ s u Q
0
a10a11 a15 a14 X Y #
! Z ; #? RO ^ ‘ * B?,? 2 #%&,’ { = w x +
- B C } 1 #,S = w x?a g 3 4 #?9? - ~ s u Q ;”,S;
! S T F – ; w x u? 0 " #
! - ; 9 ’ ‘ 8^ \? ( W,t | #? g tj # R\ %&,% (?
//,c09:Switch.java
public class Switch {
private boolean state = false;
public boolean read() { return state; }
public void on() { state = true; }
public void off() { state = false; }
} ///:~
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 22
a8 a9 22 a8
//,c09:OnOffException1.java
public class OnOffException1 extends Exception {}
///:~
//,c09:OnOffException2.java
public class OnOffException2 extends Exception {}
///:~
//,c09:OnOffSwitch.java
// Why use finally?
public class OnOffSwitch {
private static Switch sw = new Switch();
public static void f()
throws OnOffException1,OnOffException2 {}
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions..,
f();
sw.off();
} catch(OnOffException1 e) {
System.err.println("OnOffException1");
sw.off();
} catch(OnOffException2 e) {
System.err.println("OnOffException2");
sw.off();
}
}
} ///:~
R v ;Q F
′? " #( 7 0 #,S
! ! ~/ # B? S T? \?Z 7 8%& + - ’ ~L ^ \ ’?Z #,S
! !? ’ ~/ x
! ) y #– S # B? 8,! ^\ @?
//,c09:WithFinally.java
// Finally Guarantees cleanup,
public class WithFinally {
static Switch sw = new Switch();
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions..,
OnOffSwitch.f();
Thinking in Java 3rd Edition
a7 23
a8 a9 23 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
} catch(OnOffException1 e) {
System.err.println("OnOffException1");
} catch(OnOffException2 e) {
System.err.println("OnOffException2");
} finally {
sw.off();
}
}
} ///:~
1
! !,/ ^ \ @ # R # * 0?.’ Q
Z - / F * RK
N?,? #! $ ~?Z 7 8 2 r ^ K ( D E ! ( d? e 7 8%& ) * #
//,c09:AlwaysFinally.java
// Finally is always executed,
import com.bruceeckel.simpletest.*;
class FourException extends Exception {}
public class AlwaysFinally {
private static Test monitor = new Test();
public static void main(String[] args) {
System.out.println("Entering first try block");
try {
System.out.println("Entering second try
block");
try {
throw new FourException();
} finally {
System.out.println("finally in 2nd try
block");
}
} catch(FourException e) {
System.err.println(
"Caught FourException in 1st try block");
} finally {
System.err.println("finally in 1st try block");
}
monitor.expect(new String[] {
"Entering first try block",
"Entering second try block",
"finally in 2nd try block",
"Caught FourException in 1st try block",
"finally in 1st try block"
});
}
} ///:~
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 24
a8 a9 24 a8
-? X #! X? $ ~? #-
! S T * v?* v ) y #; X?
g #
Z 7 8? 1 ! - ^ \?Z %& vw #? – =?Z #+ R ~- ’? = ^ \?Z F – > 2q e @ A
! ) y #R 0?
//,c09:LostMessage.java
// How an exception can be lost,
// {ThrowsException}
import com.bruceeckel.simpletest.*;
class VeryImportantException extends Exception {
public String toString() {
return "A very important exception!";
}
}
class HoHumException extends Exception {
public String toString() {
return "A trivial exception";
}
}
public class LostMessage {
private static Test monitor = new Test();
void f() throws VeryImportantException {
throw new VeryImportantException();
}
void dispose() throws HoHumException {
throw new HoHumException();
}
public static void main(String[] args) throws
Exception {
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose();
}
monitor.expect(new String[] {
"Exception in thread \"main\" A trivial
exception",
"\tat
LostMessage.dispose(LostMessage.java:24)",
"\tat LostMessage.main(LostMessage.java:31)"
}); }
} ///:~
Thinking in Java 3rd Edition
a7 25
a8 a9 25 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
T 1 ( H #9 ˙ | /
! 2 2 J R ^ \ F #9 R\?Z / # !? D E ! #R~ g& %&,# I S z? #
~ R? {^ \?Z 7 8# ' {} \?Z 1 ‘ #F? d % tU ‘
~ i R\3 4 £ ^ @ #< Z – D,-
),- ’ L?Z @ n#,9,N?!
+@ n " #–5 ’ L R\@ n !,?Z R
^ \- o 2 # M ’ 9 o # 9 L?Z? M
R? § R\6 N 3? R?
~ !Z g o 2?
//,c09:StormyInning.java
// Overridden methods may throw only the exceptions
// specified in their base-class versions,or
exceptions
// derived from the base-class exceptions,
class BaseballException extends Exception {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}
abstract class Inning {
public Inning() throws BaseballException {}
public void event() throws BaseballException {
// Doesn't actually have to throw anything
}
public abstract void atBat() throws Strike,Foul;
public void walk() {} // Throws no checked
exceptions
}
class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}
interface Storm {
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}
public class StormyInning extends Inning implements
Storm {
// OK to add new exceptions for constructors,but
you
// must deal with the base constructor exceptions,
public StormyInning()
throws RainedOut,BaseballException {}
public StormyInning(String s)
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 26
a8 a9 26 a8
throws Foul,BaseballException {}
// Regular methods must conform to base class,
//! void walk() throws PopFoul {} //Compile error
// Interface CANNOT add exceptions to existing
// methods from the base class,
//! public void event() throws RainedOut {}
// If the method doesn't already exist in the
// base class,the exception is OK,
public void rainHard() throws RainedOut {}
// You can choose to not throw any exceptions,
// even if the base version does,
public void event() {}
// Overridden methods can throw inherited
exceptions,
public void atBat() throws PopFoul {}
public static void main(String[] args) {
try {
StormyInning si = new StormyInning();
si.atBat();
} catch(PopFoul e) {
System.err.println("Pop foul");
} catch(RainedOut e) {
System.err.println("Rained out");
} catch(BaseballException e) {
System.err.println("Generic baseball
exception");
}
// Strike not thrown in derived version,
try {
// What happens if you upcast?
Inning i = new StormyInning();
i.atBat();
// You must catch the exceptions from the
// base-class version of the method,
} catch(Strike e) {
System.err.println("Strike");
} catch(Foul e) {
System.err.println("Foul");
} catch(RainedOut e) {
System.err.println("Rained out");
} catch(BaseballException e) {
System.err.println("Generic baseball
exception");
}
}
} ///:~
– S
( s t u Q? $ @ n," ~L ^ \?Z #+? a g 9,?Z $ L R m f n #U B–’ 2? · K 2 - ’ ~
$ +? 1?Z R n$ ’ k
@ n# 0
! Z - J #? 9 z \@ n#^ \ (
$ #^ \ # Rz \@ n.~L ^Z #
) F ( ( i?
Thinking in Java 3rd Edition
a7 27
a8 a9 27 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
" #– ~0 1 $ #?
( $?Z | R$ - M 8 #? # – R2?Z " # L M? 7 8 F x #@ n
! ! e,#? 2 #K L?Z " #K R\3 4
RZ @ o 2 s t u Q? – S b
( #o s t u Q L —?Z #9 5 ~ V 9 ;L?Z x #′k o s t u Q ) ~S RO
tK O A,s t u Q R,| s t u Q #o s t u Q?Z a ! #9 - ’ ~L U s t u Q,L?Z? #o s t u Q ’ ′ s t u Q L
Z
( ),S ’ ! < = #? 9 ~L?Z #
( ~ ! 1 U –R m? #– ’,
( " m?Z 7 8 + F – 9 l?
( o ‘ #R\@ n - ’ ~L?Z #k %& < = 2 o ¥ @ n?Z a # ‘ l
+y
$ @ n? #o @ n S L?Z #4 9,e,?Z R? #4 @ n~L?Z #RO m$ ~? - %&?$ 1
0 h g #9 L
% - #R\?Z? ~/ 0 L
- RO # –+ (? M #i?,9
0? #– e ’ - % - ′- o ‘ #Z 7 8%& $ ’
% - y ^ \B Z [ J @
R –’ # –
( ‘ !? # ! 1 5 ~ 2 ;3– R\,L?Z + –D9 g I #K ! 1 ~;3 –?Z,- R2o 2,’ '
a10a11 a16 a14
= %! # ! 1 ~?Z a? 2 ;3 #+?Z a h i
j k @ n q k ’ 0 #q k ′@ n Q — K? 8 R^ fl~ Z -? #– ’ V?Z a ‘ @ n #^ \ 1 @ n?Z a !?Z # ^ e ~ 1 o @
n?Z a Rfl? - #? ! # @ n 1? l ^ a # + = %! #@ n?Z a |? ˇ? u RP?|?
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 28
a8 a9 28 a8
! ! ! !?Z % " #- ^ fl Z ;# – #
R 0?Z #9 ~ 8 T? ˇ [ Q " #–,F #+ s t u Q \6 s t u Q? ‘ u ^? | 0 #+ 9? ’ – ^ 2U 9? # ( ^ \W,R.
2? ‰ K \ ‘ #i?,q r 8@ n ) * ~ 8 s t u Q L?Z #K R2 8$ nP Z (? R? –+s t u Q " # q? fi
′k –5
! # ’ ~? S 9 ‘ R\3 4 +? a g K n o #?
! ~,8 #4 – ;9 " 9 $ ~? # –! ;
! ‘ – 8? #– u ^ 2v w # s t u Q P Z ′? u e v w #k – ;
! ‘? 8 ′k R? @ n q? ¢ £ – Bz \ @ %& v f ‘
#? M - n# ¥ R n
! – 8 Rcurrency1 %&? b ^ \ ’? ( i `
X l? W,
( - ‘ 9 £,; { ( 7 % v y 4 !
-?0 ! ! # = Rz \,no #? ;’ 8 9,§ ˇ? 3 4?
//,c09:Cleanup.java
// Paying attention to exceptions in constructors,
import com.bruceeckel.simpletest.*;
import java.io.*;
class InputFile {
private BufferedReader in;
public InputFile(String fname) throws Exception {
try {
in = new BufferedReader(new FileReader(fname));
// Other code that might throw exceptions
} catch(FileNotFoundException e) {
System.err.println("Could not open " + fname);
// Wasn't open,so don't close it
throw e;
} catch(Exception e) {
// All other exceptions must close it
try {
in.close();
} catch(IOException e2) {
System.err.println("in.close()
unsuccessful");
}
throw e; // Rethrow
} finally {
// Don't close it here!!!
}
}
public String getLine() {
Thinking in Java 3rd Edition
a7 29
a8 a9 29 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
String s;
try {
s = in.readLine();
} catch(IOException e) {
throw new RuntimeException("readLine()
failed");
}
return s;
}
public void dispose() {
try {
in.close();
System.out.println("dispose() successful");
} catch(IOException e2) {
throw new RuntimeException("in.close()
failed");
}
}
}
public class Cleanup {
private static Test monitor = new Test();
public static void main(String[] args) {
try {
InputFile in = new InputFile("Cleanup.java");
String s;
int i = 1;
while((s = in.getLine()) != null); // Perform line-by-line processing here..,
in.dispose();
} catch(Exception e) {
System.err.println("Caught Exception in main");
e.printStackTrace();
}
monitor.expect(new String[] {
"dispose() successful"
});
}
} ///:~
( - s t u Q ;^ \? Q #R\ –;? ( K \W,W,
B? R\W,? b ^ \
- - i q? - #–5 9 ·? b ^ \!P · –?
0 ! !? ( - 7 ) ^ #9 DRz ′ f ‘

- s t u Q (? g #9 ~L ^ \
- + -?Z # R\?Z o currency1 7 8 F – R\?Z " #i ; W,#? R\W,? -? ( U 9?Z
N? W,#? 9,?Z ) * #W, (
F x # - U 9 @ n’ L
- + - #R\@ n - 2',R #–? D9 ’? ^ \
B? @ n$ ’ ~L?Z #
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 30
a8 a9 30 a8
,S #? ; ^ K,! 1 Y #R5 = R [ ^ m ‰ 7 8) y #?Z / L # k s t u Q Y R m f E #? –) z { ·
” @ #B9 R\ ‘ b ‰? # S fi R\6 N *,% u c v w fl currency1 #′k
! ~ ‰? s t u Q ) y,^ … #? 9?
W,@ £,z { W,
( - ‘ \ W w,7 k? ( 0 #,SR m e f E
" @ n~o p W,^ w – 9,’ L?Z
" #+ R\?Z? 7 8 #?
" ~L?Z?Z u @ - ^ \? 4 #?Z > R^ K 7 8? R 7 8^ > ’ #x y g K L
t?Z? ˙ |? g Lf E #˙ | g L F x’ n % = R #
" @ nD?Z X l
#S %
@ n;′? ‰ ( - ‘ ) y,RO ’=
0 ! !?4t- ‘,"? ˉ ·
W, – ‰ ( - ‘ ) * ~” 9 #,S1 – ;R\ ’ –~ g & g ’
! # #+ £,? {
-7 % = #– L M ! # ~ ~/,4 /,#–$ L M 9 ~ ",
R £ ^ a12,- 8 M w x 8),~ 0 #,S – 0?%& #,$ - W #tj
! # " ; { 8$ ~0
* & $,? b ( -? ( R\%& W,#x y ^ ^ ` W,#? g? x £,$ S? ^ fl @ n#+ R,-?Z,′
R\6 N? - ^ \ 7 #9 – = # 5 ; R 8 9?Z
7 8?Z? # [ * 4 n?Z
′ f,#q? ! 1 $ ~? 2 ;3 #Z ^,L? #– 5 ’ =
" #" #" #" #
L?Z " #?Z 7 8? ˉ ~? … –+ [ & e 7
8%& e v 7 8%& ) y #9?Z? 7 8 #k currency1 e? $ ′?
1
a17a19a18a21a20a23a22 a24 a25 a26a28a27 a29 a30a24 a31 a29a33a32a25a28a34 a35 a36 a35 a37a39a38a41a40a39a42a44a43a39a45a44a46a39a42a47a22 a32a26a28a25a48a29 a30a24 a31 a29a13a25 a49 a35a28a50 a25 a30a27a51a37a44a52a54a53a39a55a44a56a58a57a41a59a39a60a44a53a39a61a44a62a21a63a64a38
Thinking in Java 3rd Edition
a7 31
a8 a9 31 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
v " i ;3 L?Z 7 8%&,;3?Z ‰ v o ‘ $ S v 7 8%& ! # R\%&?
//,c09:Human.java
// Catching exception hierarchies,
import com.bruceeckel.simpletest.*;
class Annoyance extends Exception {}
class Sneeze extends Annoyance {}
public class Human {
private static Test monitor = new Test();
public static void main(String[] args) {
try {
throw new Sneeze();
} catch(Sneeze s) {
System.err.println("Caught Sneeze");
} catch(Annoyance a) {
System.err.println("Caught Annoyance");
}
monitor.expect(new String[] {
"Caught Sneeze"
});
}
} ///:~
# ~/ {^ \ v N? #F x $ %& {^\ x –DR\
‰ #5 8?
try {
throw new Sneeze();
} catch(Annoyance a) {
System.err.println("Caught Annoyance");
}
%& x ’ ( #? R? 9
# l Y ) #
/ ~ / S T,- ′9 o ‘?Z R^ fl Z - # – @ n? g [ o?Z? #5 ;? %&?Z #K,

N? * # ~ o?Z J # RO?
try {
throw new Sneeze();
} catch(Annoyance a) {
System.err.println("Caught Annoyance");
} catch(Sneeze s) {
System.err.println("Caught Sneeze");
}
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 32
a8 a9 32 a8
RO ! 1 ~0 1
# N? @ $ #? 9 J–/ ^ \
$ % & ’$ % & ’$ % & ’$ % & ’
Z 7 8? ˉ? ^ ` ’ B– %& P Z ( [ & ′ F >
Z? 1 " #R\ ( | #k %& $ ’ #tj $ ;? … P Z [ & ·?Z ^ 2 F * @ n n78? ),S ;( 0?Z 7 8? ˉ #? #; u Q,
\,m § currency1? Y ¢ #,S %&,· m ′?,$ J6 7 s? #( 0?Z 7 8 p y @ %&
Z 7 8 ^ \ ;y – L M? 7 8R\?Z #–
· 9? a g #?Z 7 8- ^? ; v #R D7 8?Z b?Z 0 @ ( RO – ’ ^ \ @ !? ¢ · –? 3 4 #x y £ ^ @ · 7 8R2?Z 3 4 RO %&
~/?Z 7 8R ˉ? 3 4 Jh ˙ ¨ #k %& $ k 8?
+ #
$ " DR,i 2 ^ fl#R? 4 – y? R m # ! 1 $ ~ 2 ;3 –
# y? g $N? k #R
$ ) ! 0 ! 5 5 6 3 4?
try {
//,.,to do something useful
} catch(ObligatoryException e) {} // Gulp!
%&,
£ # 5 {^,n o ¢ m #k R\?Z / < Z - ;R? #+ 5 - RO 6 ’ ! < = #,S M – p ‘ P R2 #?Z ~?Z
0 = #+ / – #? 9? H? x ! 1 ;3 –c +?Z 7 8%& #K 4 R,- #;T @? #? …?
F £ … £? R ˇ ^ \ " #£ n ˙ f ^ ˇ J #| {}
! #£ 7 8%&? ˇ ) @ n‘ R\3 4 7! [ 6 N? R\? n#R\m n? f E
RO S,H?Z #+ –? L M? 7 8?Z R^? #£,
Thinking in Java 3rd Edition
a7 33
a8 a9 33 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
‘ $ " 3 4?9 i 0 — #S T? @n‘ R23 4
R\? 4 ‘ n o #+? a g i 2 # ; 9? Z [
) - ~? c I # " P Q $,p ] Y £ z ),S ~- R? * fl#? £,,<?
7 v y * K O — X Y # 8 #
’ 0 ’ #? RO — X Y ’ 9
# 8 # ’ 0 ’ #$ !? — § currency1 X Y #R * j,n F –5 ( dR\X? "
£ ^ O #~0 1 9* ‘ 7 K O # — § currency1 ’ ^ @,- 3 4 R #£? ′ f £ … = %#0 –£ N O b — § currency1?
G F x # [ " 9? Z - #+ F 9 m £,· \ i " #£,= · 5 R' o i
£ ^? Y?,- —,+ - 2 ’
Z 7 8 V 1
3 4?/ )? ˉ #y ‘ R 1 3,#
) " #/ 0 8;# #< ! ! # # # $ # S T
y = 0 #? > u #5
u j,· ^ 2,u - 3 4 ’ %&,;^ \,? 7 8?h i a #?
Z 7 8 6 /? – ′ u j # 0 0,
v y = %?Z — ; n 3,x F U 9 X YZ 7 8?
# ) " z j,-?Z 78#+,-?Z a
#S T / 0 8; 9? - -?Z $ - a
3 "? #,? / 0 ! a65a66 a66a33a67ˉ #?
! $ / 0 X Y - ^ \ ;? #?
@ ! " # $ % & ’ ( ) * +,-,/ 0 1 2 3 45 6 / 0 1 7 8 9 2 3
#?Z 7 8 p y ; R? o 2 #+ £,R b
$ " R?,? @ M?
@,$ ; < 2 3 = >? @ A - B C 2 3 8 9 $ % & " D E 3 F G H 2 3 8 9 B I J K
Z u … 3,0 0 " U v ;? $ h i,£? R? a JK 2 < Z?,+
Z 7
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 34
a8 a9 34 a8
8 %&,#? ; R [ R [ @,S,+
%& o #6 7,- #x y
0 ’ ’ ‘ R2%&,L M #?Z?,;+^ 2< Z + [ ‘? #;,7 8 P Q ( M g # [ ‘ ’ [ £ #
$ " " #R^ fl ;
b 3,K? * = ‘ £ ^ @ n q k !? R\@ n ’ ~L a 2?Z?Z a?Z a - z \ _ 9 a £ ~' RZ #– 7 8 £ ^ K £ 6 7 R2
Z #9 ;′–‘ 7 8 o?Z 2?X n " #£,^ ˙? –‘ 7 8 R> ’ #+ R £ q? Z [ J R ^ \¢? #£,<Z,6 7?Z a,Z
Z a j k u Q q k !,^ ;§ currency1 R2?Z a * y ^ # ^ \u Q t@ n~L > 2?Z #K 9 to $ $ L O?Z
#9 ~ ! – § currency1 S Q e u Q t@ n ! L?Z #tj?Z a ‰
$ a #?Z a - -? Q &,- ~/ L?Z R?§ currency1? - #+ 5 ( L?Z f?Z a #
~” v y @ n - J #′k
) # v y -?Z a ′ ‘ #?Z a ~
' Z ˇ
# { % &,A! 1
_ ;ˉ #
¢? g 0 $ " G
Z a | 0 # $? 9 0 1 %&,^ _ ~ Y
R? ^ \ # ) *? -? X Y? R? m n j #? b? 6 N?u %& ‘ #
$ " - 7 + F %& ( d " #9 ~* ‘ ^ 2I F x #%& ^ ˇ 9 ˇ E k ˇ? X Y ~/ k ~?
u? RO? x - ^? #–~0 1? S 8 #1 n 8 R £,a = [ — § currency1 #q?
$ "
,t? 3 4 ‘ %& \ˇ? 4 ′k [ *,u %& ‘ m 3? #? R
\3 4 - \ > u j 0 1? L M N O P Q $ R S 2 3 T U # V W X < Y - ) * Z [ \ ]
^ _ ‘ a b c d e f g h i /? K j j X < ) * k % K l\] ^ _ m ! n / o p Z [ q r s t Z [
a65a66 a68 a67
Thinking in Java 3rd Edition
a7 35
a8 a9 35 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
/?Z " # 3,u,a M?
u,v @ w -,/ x p y z { | } ~ Z 8 9 b/
a65a66 a69 a67
0 0 RO = ^ \ -?Z a @ n S L?Z?
a b ! = > i Z 2 3 T U K b i @
K l $? $ -,7 2 3 8 9,$ 2 3? Y i ¢1 / £? 2 3 b $ ¥? § currency1 ',?
a65a66 a70 a67
£,? - t?Z? #
$ " g
/ B 5 9 9? j
J£ + Rcurrency1?
@ T u v 2 3 fi / fl a b –? –? ·,? … ‰? ` ′? 8?
£ z
F? ) ˉ ^ U / 0 A #RO,-,’ < =?Z ˉ?
- R m #R? 9 ; y – #;… K 2˙ | 6 7,-
+ –^?Z ‘ /0 #K 5 ;? S?Z # #K R2 ~/ g K
2 tU 9 K %& ’ ) #
— #Z? / 0,^ @ A #,S $ "
o 2? K = · #£ e
$ " — § currency1 ( 0 %& Z ; + #£?£ h
a65a66 a71 a670 £ #R2 7? a g ‘ k?
,!"#
(,$%&’()*+,-.*+#/0 1 23 45 -,67% $ ()8 9:( ;<=>?#
#? $ ! o 2 ’ r %& %? a g #
a? -,‘ — § currency1,* ‘ = [ o 2 # –~ ^ 7 S T 5 [ 6 N !,l
- ˉ W #,a #R?d a #R? Y * ~ £ #
~BW? #~ r 6? ',G § !
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 36
a8 a9 36 a8
ˉ,- #RO 6 ’ ˇ? #R? G § S a e U? ; ;8 ! 1 ’ ¢ o {
A7 #£,!
0 ) 0?o P P,0 ’ ; #,-,a? X n #9,g a ¢ g Rcurrency1? a 8 Y?
# ˙ - C? a b t h ¨? /? Q 7 J
a65a66 a72 a67 N a #;B
$ " · #R? ’ ‘,U,X Y ‘ a #R\? ’ – 2#
0 j,$ Z ˇ
0 - ‰ y –? % \ ¢ J– #? a g,-
0,,’ 0,g # * 9 - [ - + # –0 1 - 2
$ " m – \ #tj –0 1 – · –,L M? 7 8?Z #K? - 2?n
^ 2n o %& # R 5 [ 6 N #;?Z R +
[ n o? n D9,# 2 ·? # –? ` W,9? (
{ ( 7 ~? 8 9 #RO ;
- ( # 9 ~L?Z ^ 2n o %& #– SRO m
– S 5 [ @ R? n?
//,c09:MainException.java
import java.io.*;
public class MainException {
// Pass all exceptions to the console,
public static void main(String[] args) throws
Exception {
// Open the file,
FileInputStream file =
new FileInputStream("MainException.java");
// Use the file,.,
// Close the file,
file.close();
}
} ///:~
@ n$ S -?Z a #R L,- $ "
Z / I 2 #–
,N? g #W,4 ; R i 2 [ #,S ‰ {
( 7 ) *? r [ V
Thinking in Java 3rd Edition
a7 37
a8 a9 37 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
+ " #L?Z @ #+ R? @ n < ! P; +A < @ n –” £ ^ \@ n#x y 0 1 £ L M N O 78R\?Z #+ R ’ 9 #tj? ^ 2l H
% &,-?Z F ; ^? \ ‘ R\3 4 –˙ |
$ "? – # RO?
try {
//,.,to do something useful
} catch(IDontKnowWhatToDoWithThisCheckedException e)
{
throw new RuntimeException(e);
}
–?
$ " g ’? #R g · ^\? n –Z #$ 9 @ n?Z a #Z F? ’ { – ~?Z? dG H
R?,J –^?,- #– S +
,NZ a #˙ | 6 7?Z #B9,
"? g #–? S
* i 7 8R\?Z # R?
//,c09:TurnOffChecking.java
// "Turning off" Checked exceptions,
import com.bruceeckel.simpletest.*;
import java.io.*;
class WrapCheckedException {
void throwRuntimeException(int type) {
try {
switch(type) {
case 0,throw new FileNotFoundException();
case 1,throw new IOException();
case 2,throw new RuntimeException("Where am
I?");
default,return;
}
} catch(Exception e) { // Adapt to unchecked,
throw new RuntimeException(e);
}
}
}
class SomeOtherException extends Exception {}
public class TurnOffChecking {
private static Test monitor = new Test();
public static void main(String[] args) {
WrapCheckedException wce = new
WrapCheckedException();
// You can call f() without a try block,and let
// RuntimeExceptions go out of the method,
wce.throwRuntimeException(3);
// Or you can choose to catch exceptions,
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 38
a8 a9 38 a8
for(int i = 0; i < 4; i++)
try {
if(i < 3)
wce.throwRuntimeException(i);
else
throw new SomeOtherException();
} catch(SomeOtherException e) {
System.out.println("SomeOtherException,"
+ e);
} catch(RuntimeException re) {
try {
throw re.getCause();
} catch(FileNotFoundException e) {
System.out.println(
"FileNotFoundException," + e);
} catch(IOException e) {
System.out.println("IOException," + e);
} catch(Throwable e) {
System.out.println("Throwable," + e);
}
}
monitor.expect(new String[] {
"FileNotFoundException," +
"java.io.FileNotFoundException",
"IOException,java.io.IOException",
"Throwable,java.lang.RuntimeException,Where
am I?",
"SomeOtherException,SomeOtherException"
});
}
} ///:~
’ * S? U 9 —?Z R2?Z / –
‘ #,S 9,? R2?Z
0
) ! ! * S B?,
#? 9 L $ "
+ #F – e ·?Z " #–? S B?‘ –Z –
# B? e ~L?Z #R
) ; y · x y
* ′? $ K \?Z L ‘ RO? K \?Z J ‘ #x y S 9,
N? ‘ 7 8 5 U i > ’ ~? _ R?
‘ 9
$ "
Thinking in Java 3rd Edition
a7 39
a8 a9 39 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
Z ‘?
1,f E @ 7 83 4 (? ¥? L M? 7 8? ·?Z
)
2,3 4 #x y,K \C 3 4 @ n
3,P ^ 3 4 #x y = K \@ n? @ ·
4,^ 2? # y? BR\@ no p Q ‘ –?
5,F * ( D E ’ m ¢ > m ‰ #x yZ L rK
6,F * ( D E ’ m ¢ > m ‰ #x y L ^ \/Z rK
7,! = %&
8,n (Z ′ s ¢ h i 2 #K ‘ ~ Z ! $
)
9,?%& m (R?,,? ’ W ' · #$ %&? W ' ·
)
‰ " h i
# ^? ’? %& < h i %& ‘ a #,; #+
‘ a #9 ; #?
_ ; v ;? b ;? %& K,l@ ˇ — ˇ — < =?Z #; ^? ˉ ^ / 0 A #R 9 ’ DK,,0 3 4 # I q J?
Z 7 8 ;B£,’ 1 $ #S ^? no @ A ‘ ( 0 ˇ — # # %& #i? B– ( 0 = %! ’ G – %& - 7 8?Z q? I #+ $ ’ J?
* ‘ c % l?
5 ; u ^ & Y ’ b
’ N W ^ #Rg -^ 2o 4 p ]
b ^ \ # B? L ^ \ I ^ \
Q J N? R\?Z #x y? R\
Q y? ^ \ N? #B9? ^ 2G H S { % = K
b ^ \–?Z R\ +^ \* Q s t u Q #i? B ‘ x R\
+^ \~ R\
Chapter 9,Error Handling with Exceptions
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a7 40
a8 a9 40 a8
‘ @ n? b ^ \ N? #P,^ R\?Z
+^ \ ( ’ L ) o,? b?Z @ n?Z a # ’ ! < =? g?Z a
N? P,^ R\Z
e,^ \ ‘ # 9 p d R\,‘ @ n x y
N? Rcurrency1 ‘ S?Z
b ^ \*? Rz \@ n L ^? – e,?Z
,#?Z ) y ; N? L £ ^Z
–e,{}Z? P,
i * ^ \) o #+ N? #;
Z ‘
b +Z +^ \* ’ L R+Z @ n
,R\@ n#+ 5 ’ - ^ ' N? # 9 ;’ R+Z
+^ \~C 0 i? L %&
D? b ^ \ h i —?Z 7 8%& #B9 i ( #˙ L?Z
b ^ \+ KZ? x y? b ^ \* ’ L?Z @ n
#i? +R\@ n#B9 L {} K?Z? @ · #B
b ^ \ ‘ #g I
#x y,R\@ n
{ ^ #o s t u Q 9 s t u Q,L?Z
3? ^ # B? L ) y
!" # ~( *
3? ^ # B? L ) y
$ % !" # ~( *
# ) o #? ^ \ N { ^ #4 L
& ’? # N? $ ~
s t u Q R^? {} currency1,a @ n#? b ^ \ v ‘ 2,8 6 N
# !" # #? ^ \(?Z #? ^\~L RZ @ n P,^ # y
‰ M {^ \ N? #? { ^ # x ’ P Z !?(
B) * !" # [ ^ K?Z # {+ \?Z ‘
+ +
{ 7,!" #? ^ b f E?Z
{ 7? ^ b f E?Z
^ \ x W,‘ l * !" # W,( %& #? ^ ( ′?
Thinking in Java 3rd Edition
a7 41
a8 a9 41 a8
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
a73a74 a75 a76
%&,5 ;· o p s # ~L M £ - + ˇ U?
a73a74a33a77 a76
Z 7 8 - ! # N? #? 9 ; ~ s 1 0 ‘ ‰? 8<?
a73a74 a78 a76
~ s u Q ^? ‘? ‰ ) y,u Q –) ’ Q L M # # fl” ~ s u Q
~,~ s 1 #
> 9 - ~– 8 2
a73a74 a79 a76
g o 2 #;3 o @ n,L?Z ; @ n #tj @ nL?Z o R
! P’ G ! –?Z a § currency1 @
a73a74 a74a33a76
3 "? #? 3,?Z 7 8
# < < < 2 ! 5 < ’ ’ #
.,< 8A# 7,# * o,R- / 0 g e #5 -,#,S –5 ’ /? T 5 0 e ^ - R S
a73a74 a80 a76
$ C 44 0,,) 4 $ 45,D ( E F 3 E
% 2 7 < 2 F E = ;( + (
a73a74 a81 a76
g
a73a74 a82 a76
0 0 9 ! ! " # $ a83
a84
# % &’ % ( ) * * + { ;* 1,
a73a74 a85 a76 | b [ -?
) " %&? ! ˙ | k
# $ " (
a73a74 a86 a76
& & 9 % 3 X Y u j # < ! ! X Y u j
/ # $ C 445 5 5,,) 4 4 4 4 ) 4 ’ $ 4