📄 chap10.htm
字号:
does throw that exception. This has the beneficial effect of being a placeholder
for that exception, so you can actually start throwing the exception later
without requiring changes to existing code. It’s also important for
creating <B>abstract</B> base classes and <B>interface</B>s whose derived
classes or implementations may need to throw exceptions.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I34'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I35>
</FONT><A NAME="_Toc312374118"></A><A NAME="_Toc375545368"></A><A NAME="_Toc481064721"></A><BR></P></DIV>
<A NAME="Heading340"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Catching any exception<BR><A NAME="Index1075"></A><A NAME="Index1076"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is possible to create a handler that
catches any type of exception. You do this by catching the base-class exception
type <B>Exception</B> (there are other types of base exceptions, but
<B>Exception</B> is the base that’s pertinent to virtually all programming
activities):</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>catch</font>(Exception e) {
System.err.println(<font color=#004488>"Caught an exception"</font>);
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This will catch any exception, so if you
use it you’ll want to put it at the <I>end</I> of your list of handlers to
avoid preempting any exception handlers that might otherwise follow it.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I35'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I36>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since the <B>Exception</B> class is the
base of all the exception classes that are important to the programmer, you
don’t get much specific information about the exception, but you can call
the methods that come from <I>its</I> base type
<A NAME="Index1077"></A><A NAME="Index1078"></A><B>Throwable</B>:</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>String
getMessage( )</B></FONT><BR><FONT FACE="Georgia"><B>String
</B><A HREF="C:#getLocalizedMessage()">getLocalizedMessage</A><B>( )</B></FONT><BR><FONT FACE="Georgia">Gets
the detail message, or a message adjusted for this particular locale.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I36'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I37>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>String
toString( )</B></FONT><BR><FONT FACE="Georgia">Returns a short description
of the Throwable, including the detail message if there is one.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I37'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I38>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>void
<A NAME="Index1079"></A>printStackTrace( )
</B></FONT><BR><FONT FACE="Georgia"><B>void
printStackTrace(PrintStream)</B></FONT><BR><FONT FACE="Georgia"><B>void
</B><A HREF="C:#printStackTrace(java.io.PrintWriter)">printStackTrace</A><B>(</B><A HREF="C:">PrintWriter</A><B>)
</B></FONT><BR><FONT FACE="Georgia">Prints the Throwable and the
Throwable’s call stack trace. The call stack shows the sequence of method
calls that brought you to the point at which the exception was thrown. The first
version prints to standard error, the second and third prints to a stream of
your choice (in Chapter 11, you’ll understand why there are two types of
streams).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I38'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I39>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Throwable
</B><A HREF="C:#fillInStackTrace()">fillInStackTrace</A><B>( )</B></FONT><BR><FONT FACE="Georgia">Records
information within this <B>Throwable </B>object about the current state of the
stack frames. Useful when an application is rethrowing an error or exception
(more about this shortly).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I39'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I40>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In addition, you get some other methods
from <B>Throwable</B>’s base type <B>Object</B> (everybody’s base
type). The one that might come in handy for exceptions is
<A NAME="Index1080"></A><A NAME="Index1081"></A><B>getClass( )</B>, which
returns an object representing the class of this object. You can in turn query
this <B>Class</B> object for its name with <B>getName( )</B> or
<B>toString( )</B>. You can also do more sophisticated things with
<B>Class</B> objects that aren’t necessary in exception handling.
<B>Class</B> objects will be studied later in this book.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I40'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I41>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here’s an example that shows the
use of the basic <B>Exception</B> methods:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:ExceptionMethods.java</font>
<font color=#009900>// Demonstrating the Exception Methods.</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> ExceptionMethods {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>try</font> {
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> Exception(<font color=#004488>"Here's my Exception"</font>);
} <font color=#0000ff>catch</font>(Exception e) {
System.err.println(<font color=#004488>"Caught Exception"</font>);
System.err.println(
<font color=#004488>"e.getMessage(): "</font> + e.getMessage());
System.err.println(
<font color=#004488>"e.getLocalizedMessage(): "</font> +
e.getLocalizedMessage());
System.err.println(<font color=#004488>"e.toString(): "</font> + e);
System.err.println(<font color=#004488>"e.printStackTrace():"</font>);
e.printStackTrace(System.err);
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The output for this program
is:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Caught Exception
e.getMessage(): Here's my Exception
e.getLocalizedMessage(): Here's my Exception
e.toString(): java.lang.Exception:
Here's my Exception
e.printStackTrace():
java.lang.Exception: Here's my Exception
at ExceptionMethods.main(ExceptionMethods.java:7)
java.lang.Exception:
Here's my Exception
at ExceptionMethods.main(ExceptionMethods.java:7)</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that the methods provide
successively more information—each is effectively a superset of the
previous one.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I41'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I42>
</FONT><A NAME="_Toc312374119"></A><A NAME="_Toc375545369"></A><A NAME="_Toc481064722"></A><BR></P></DIV>
<A NAME="Heading341"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Rethrowing an exception<BR><A NAME="Index1082"></A><A NAME="Index1083"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Sometimes you’ll want to rethrow
the exception that you just caught, particularly when you use <B>Exception</B>
to catch any exception. Since you already have the reference to the current
exception, you can simply rethrow that reference:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>catch</font>(Exception e) {
System.err.println(<font color=#004488>"An exception was thrown"</font>);
<font color=#0000ff>throw</font> e;
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Rethrowing an exception causes the
exception to go to the exception handlers in the next-higher context. Any
further <B>catch</B> clauses for the same <B>try</B> block are still ignored. In
addition, everything about the exception object is preserved, so the handler at
the higher context that catches the specific exception type can extract all the
information from that object.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I42'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I43>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you simply rethrow the current
exception, the information that you print about that exception in
<A NAME="Index1084"></A><A NAME="Index1085"></A><B>printStackTrace( )
</B>will pertain to the exception’s origin, not the place where you
rethrow it. If you want to install new stack trace information, you can do so by
calling
<A NAME="Index1086"></A><A NAME="Index1087"></A><B>fillInStackTrace( )</B>,
which returns an exception object that it creates by stuffing the current stack
information into the old exception object. Here’s what it looks
like:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Rethrowing.java</font>
<font color=#009900>// Demonstrating fillInStackTrace()</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Rethrowing {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> f() <font color=#0000ff>throws</font> Exception {
System.out.println(
<font color=#004488>"originating the exception in f()"</font>);
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> Exception(<font color=#004488>"thrown from f()"</font>);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> g() <font color=#0000ff>throws</font> Throwable {
<font color=#0000ff>try</font> {
f();
} <font color=#0000ff>catch</font>(Exception e) {
System.err.println(
<font color=#004488>"Inside g(), e.printStackTrace()"</font>);
e.printStackTrace(System.err);
<font color=#0000ff>throw</font> e; <font color=#009900>// 17</font>
<font color=#009900>// throw e.fillInStackTrace(); // 18</font>
}
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
main(String[] args) <font color=#0000ff>throws</font> Throwable {
<font color=#0000ff>try</font> {
g();
} <font color=#0000ff>catch</font>(Exception e) {
System.err.println(
<font color=#004488>"Caught in main, e.printStackTrace()"</font>);
e.printStackTrace(System.err);
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The important line numbers are marked as
comments. With line 17 uncommented (as shown), the output is:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So the exception stack trace always
remembers its true point of origin, no matter how many times it gets rethrown.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I43'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER10_I44>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With line 17 commented and line 18
uncommented, <B>fillInStackTrace( )</B> is used instead, and the result
is:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -