📄 chap10.htm
字号:
satisfactory.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I24'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I25>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here, the result is printed to the
console <A NAME="Index1071"></A><I>standard error</I> stream by writing to
<A NAME="Index1072"></A><B>System.err</B>. This is usually a better place to
send error information than <B>System.out</B>, which may be redirected. If you
send output to <B>System.err</B> it will not be redirected along with
<B>System.out</B> so the user is more likely to notice it.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I25'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I26>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Creating an exception class that also has
a constructor that takes a <B>String</B> is also quite simple:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:FullConstructors.java</font>
<font color=#009900>// Inheriting your own exceptions.</font>
<font color=#0000ff>class</font> MyException <font color=#0000ff>extends</font> Exception {
<font color=#0000ff>public</font> MyException() {}
<font color=#0000ff>public</font> MyException(String msg) {
<font color=#0000ff>super</font>(msg);
}
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> FullConstructors {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> f() <font color=#0000ff>throws</font> MyException {
System.out.println(
<font color=#004488>"Throwing MyException from f()"</font>);
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> MyException();
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> g() <font color=#0000ff>throws</font> MyException {
System.out.println(
<font color=#004488>"Throwing MyException from g()"</font>);
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> MyException(<font color=#004488>"Originated in g()"</font>);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>try</font> {
f();
} <font color=#0000ff>catch</font>(MyException e) {
e.printStackTrace(System.err);
}
<font color=#0000ff>try</font> {
g();
} <font color=#0000ff>catch</font>(MyException e) {
e.printStackTrace(System.err);
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The added code is small—the
addition of two constructors that define the way <B>MyException</B> is created.
In the second constructor, the base-class constructor with a <B>String</B>
argument is explicitly invoked by using the <B>super</B> keyword.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I26'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I27>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The stack trace information is sent to
<B>System.err</B> so that it’s more likely it will be noticed in the event
that <B>System.out</B> has been redirected.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I27'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I28>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The output of the program
is:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Throwing MyException from f()
MyException
at FullConstructors.f(FullConstructors.java:16)
at FullConstructors.main(FullConstructors.java:24)
Throwing MyException from g()
MyException: Originated in g()
at FullConstructors.g(FullConstructors.java:20)
at FullConstructors.main(FullConstructors.java:29)</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see the absence of the detail
message in the <B>MyException</B> thrown from <B>f( )</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I28'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I29>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The process of creating your own
exceptions can be taken further. You can add extra constructors and
members:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:ExtraFeatures.java</font>
<font color=#009900>// Further embellishment of exception classes.</font>
<font color=#0000ff>class</font> MyException2 <font color=#0000ff>extends</font> Exception {
<font color=#0000ff>public</font> MyException2() {}
<font color=#0000ff>public</font> MyException2(String msg) {
<font color=#0000ff>super</font>(msg);
}
<font color=#0000ff>public</font> MyException2(String msg, <font color=#0000ff>int</font> x) {
<font color=#0000ff>super</font>(msg);
i = x;
}
<font color=#0000ff>public</font> <font color=#0000ff>int</font> val() { <font color=#0000ff>return</font> i; }
<font color=#0000ff>private</font> <font color=#0000ff>int</font> i;
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> ExtraFeatures {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> f() <font color=#0000ff>throws</font> MyException2 {
System.out.println(
<font color=#004488>"Throwing MyException2 from f()"</font>);
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> MyException2();
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> g() <font color=#0000ff>throws</font> MyException2 {
System.out.println(
<font color=#004488>"Throwing MyException2 from g()"</font>);
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> MyException2(<font color=#004488>"Originated in g()"</font>);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> h() <font color=#0000ff>throws</font> MyException2 {
System.out.println(
<font color=#004488>"Throwing MyException2 from h()"</font>);
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> MyException2(
<font color=#004488>"Originated in h()"</font>, 47);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>try</font> {
f();
} <font color=#0000ff>catch</font>(MyException2 e) {
e.printStackTrace(System.err);
}
<font color=#0000ff>try</font> {
g();
} <font color=#0000ff>catch</font>(MyException2 e) {
e.printStackTrace(System.err);
}
<font color=#0000ff>try</font> {
h();
} <font color=#0000ff>catch</font>(MyException2 e) {
e.printStackTrace(System.err);
System.err.println(<font color=#004488>"e.val() = "</font> + e.val());
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A data member <B>i</B> has been added,
along with a method that reads that value and an additional constructor that
sets it. The output is:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I29'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I30>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Throwing MyException2 from f()
MyException2
at ExtraFeatures.f(ExtraFeatures.java:22)
at ExtraFeatures.main(ExtraFeatures.java:34)
Throwing MyException2 from g()
MyException2: Originated in g()
at ExtraFeatures.g(ExtraFeatures.java:26)
at ExtraFeatures.main(ExtraFeatures.java:39)
Throwing MyException2 from h()
MyException2: Originated in h()
at ExtraFeatures.h(ExtraFeatures.java:30)
at ExtraFeatures.main(ExtraFeatures.java:44)
e.val() = 47</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since an exception is just another kind
of object, you can continue this process of embellishing the power of your
exception classes. Keep in mind, however, that all this dressing-up might be
lost on the client programmers using your packages, since they might simply look
for the exception to be thrown and nothing more. (That’s the way most of
the Java library exceptions are used.)
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I30'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I31>
</FONT><A NAME="_Toc312374116"></A><A NAME="_Toc375545367"></A><A NAME="_Toc481064720"></A><BR></P></DIV>
<A NAME="Heading339"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The exception
specification<BR><A NAME="Index1073"></A><A NAME="Index1074"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Java, you’re required to inform
the client programmer, who calls your method, of the exceptions that might be
thrown from your method. This is civilized, because the caller can know exactly
what code to write to catch all potential exceptions. Of course, if source code
is available, the client programmer could hunt through and look for <B>throw</B>
statements, but often a library doesn’t come with sources. To prevent this
from being a problem, Java provides syntax (and <I>forces </I>you to use that
syntax) to allow you to politely tell the client programmer what exceptions this
method throws, so the client programmer can handle them. This is the
<I>exception specification,</I> and it’s part of the method declaration,
appearing after the argument list.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I31'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I32>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The exception specification uses an
additional keyword, <B>throws</B>, followed by a list of all the potential
exception types. So your method definition might look like
this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>void</font> f() <font color=#0000ff>throws</font> TooBig, TooSmall, DivZero { <font color=#009900>//... </font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you say</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>void</font> f() { <font color=#009900>// ...</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">it means that no exceptions are thrown
from the method. (<I>Except </I>for the exceptions of type
<B>RuntimeException</B>, which can reasonably be thrown anywhere—this will
be described later.)
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I32'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I33>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can’t lie about an exception
specification—if your method causes exceptions and doesn’t handle
them, the compiler will detect this and tell you that you must either handle the
exception or indicate with an exception specification that it may be thrown from
your method. By enforcing exception specifications from top to bottom, Java
guarantees that exception correctness can be ensured <I>at
compile-time</I></FONT><A NAME="fnB52" HREF="#fn52">[52]</A><FONT FACE="Georgia">.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I33'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I34>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There is one place you can lie: you can
claim to throw an exception that you really don’t. The compiler takes your
word for it, and forces the users of your method to treat it as if it really
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -