⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0098.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
f() throws tooBig, tooSmall, divZero { //... 
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you say
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">void
f() { // ...
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">it
means that no exceptions are thrown from the method. (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Except
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">for
the exceptions of type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RuntimeException</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which can reasonably be thrown anywhere &#8211; this will be described later.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can&#8217;t lie about an exception specification &#8211; if your method causes
exceptions and doesn&#8217;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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>at
compile time
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><A NAME="fnB42" HREF="#fn42">[42]</A><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
is one place you can lie: you can claim to throw an exception that you
don&#8217;t. The compiler takes your word for it and forces the users of your
method to treat it as if it really 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.
</FONT><a name="_Toc312374118"></a><a name="_Toc375545368"></a><a name="_Toc408018595"></a><P></DIV>
<A NAME="Heading289"></A><H3 ALIGN=LEFT>
Catching
any exception
<P><A NAME="Index925"></A><A NAME="Index926"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
is possible to create a handler that catches any type of exception. You do this
by catching the base-class exception type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Exception</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(there are other types of base exceptions, but 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Exception</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the base that&#8217;s pertinent to virtually all programming activities):
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">catch</font>(Exception e) {
  System.out.println("caught an exception");
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
will catch any exception, so if you use it you&#8217;ll want to put it at the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>end</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of your list of handlers to avoid pre-empting any exception handlers that might
otherwise follow it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Exception</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is the base of all the exception classes that are important to the
programmer, you don&#8217;t get much specific information about the exception,
but you can call the methods that come from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>its</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
base type <A NAME="Index927"></A><A NAME="Index928"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Throwable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
getMessage(&#160;)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Gets
the detail message. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
toString(&#160;)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Returns
a short description of the Throwable, including the detail message if there is
one.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void
<A NAME="Index929"></A>printStackTrace(&#160;)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void
printStackTrace(PrintStream) 
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Prints
the Throwable and the Throwable&#8217;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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first version prints to standard error, the second prints to a stream of your
choice. If you&#8217;re working under Windows, you can&#8217;t redirect
standard error so you might want to use the second version and send the results
to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">;
that way the output can be redirected any way you want.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
addition, you get some other methods from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Throwable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;s
base type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(everybody&#8217;s base type). The one that might come in handy for exceptions
is <A NAME="Index930"></A><A NAME="Index931"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getClass(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which returns an object representing the class of this object. You can in turn
query this 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object for its name with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You can also do more sophisticated things with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects that aren&#8217;t necessary in exception handling. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects will be studied later in the book.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here&#8217;s
an example that shows the use of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Exception</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods: (See page 
<A HREF=" PAGE#Running_programs">97</A>
if you have trouble executing this program.)
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: ExceptionMethods.java</font>
<font color="#009900">// Demonstrating the Exception Methods</font>
<font color="#0000ff">package</font> c09;

<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("Here's my Exception");
    } <font color="#0000ff">catch</font>(Exception e) {
      System.out.println("Caught Exception");
      System.out.println(
        "e.getMessage(): " + e.getMessage());
      System.out.println(
        "e.toString(): " + e.toString());
      System.out.println("e.printStackTrace():");
      e.printStackTrace();
    }
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
output for this program is:
</FONT><P></DIV>

<font color="#990000"><PRE>Caught Exception
e.getMessage(): 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 </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the methods provide successively more information &#8211; each is
effectively a superset of the previous one.
</FONT><a name="_Toc312374119"></a><a name="_Toc375545369"></a><a name="_Toc408018596"></a><P></DIV>
<A NAME="Heading290"></A><H3 ALIGN=LEFT>
Rethrowing
an exception
<P><A NAME="Index932"></A><A NAME="Index933"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Sometimes
you&#8217;ll want to rethrow the exception that you just caught, particularly
when you use 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Exception</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to catch any exception. Since you already have the handle to the current
exception, you can simply re-throw that handle:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">catch</font>(Exception e) {
  System.out.println("An exception was thrown");
  <font color="#0000ff">throw</font> e;
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Rethrowing
an exception causes the exception to go to the exception handlers in the
next-higher context. Any further 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>catch</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
clauses for the same 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>try</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you simply re-throw the current exception, the information that you print about
that exception in <A NAME="Index934"></A><A NAME="Index935"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>printStackTrace(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">will
pertain to the exception&#8217;s origin, not the place where you re-throw it.
If you want to install new stack trace information, you can do so by calling <A NAME="Index936"></A><A NAME="Index937"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fillInStackTrace(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which returns an exception object that it creates by stuffing the current stack
information into the old exception object. Here&#8217;s what it looks like:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Rethrowing.java</font>
<font color="#009900">// Demonstrating fillInStackTrace()</font>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -