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

📄 tij311.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      <font color=#004488>"%% \tat Rethrowing.f(.*?)"</font>,
      <font color=#004488>"%% \tat Rethrowing.g(.*?)"</font>,
      <font color=#004488>"%% \tat Rethrowing.main(.*?)"</font>,
      <font color=#004488>"Caught in main, e.printStackTrace()"</font>,
      <font color=#004488>"java.lang.Exception: thrown from f()"</font>,
      <font color=#004488>"%% \tat Rethrowing.f(.*?)"</font>,
      <font color=#004488>"%% \tat Rethrowing.g(.*?)"</font>,
      <font color=#004488>"%% \tat Rethrowing.main(.*?)"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The important line numbers are marked as comments. With line 17 uncommented (as shown), the output is as shown, so the exception stack trace always remembers its true point of origin no matter how many times it gets rethrown. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1551" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>With line 17 commented and line 18 uncommented, <b>fillInStackTrace(&#160;)</b> is used instead, and the result is:<br></p>

<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: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)</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>(Plus additional complaints from the <b>Test.expect(&#160;) </b>method.) Because of <b>fillInStackTrace(&#160;)</b>, line 18 becomes the new point of origin of the exception. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1552" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index817"></a>The class <b>Throwable</b> must appear in the exception specification for <b>g(&#160;)</b> and <b>main(&#160;)</b> because <b>fillInStackTrace(&#160;)</b> produces a reference to a <b>Throwable</b> object. Since <b>Throwable</b><a name="Index818"></a> is a base class of <b>Exception</b>, it&#146;s possible to get an object that&#146;s a <b>Throwable</b> but <i>not</i> an <b>Exception</b>, so the handler for <b>Exception</b> in <b>main(&#160;) </b>might miss it. To make sure everything is in order, the compiler forces an exception specification for <b>Throwable</b>. For example, the exception in the following program is <i>not</i> caught in <b>main(&#160;)</b>: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1553" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:ThrowOut.java</font>
<font color=#009900>// {ThrowsException}</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> ThrowOut {
  <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> {
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> Throwable();
    } <font color=#0000ff>catch</font>(Exception e) {
      System.err.println(<font color=#004488>"Caught in main()"</font>);
    }
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>It&#146;s also possible to rethrow a different exception from the one you caught. If you do this, you get a similar effect as when you use <b>fillInStackTrace(&#160;)</b>&#151;the information about the original site of the exception is lost, and what you&#146;re left with is the information pertaining to the new <b>throw</b>: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1554" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:RethrowNew.java</font>
<font color=#009900>// Rethrow a different object from the one that was caught.</font>
<font color=#009900>// {ThrowsException}</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> OneException <font color=#0000ff>extends</font> Exception {
  <font color=#0000ff>public</font> OneException(String s) { <font color=#0000ff>super</font>(s); }
}

<font color=#0000ff>class</font> TwoException <font color=#0000ff>extends</font> Exception {
  <font color=#0000ff>public</font> TwoException(String s) { <font color=#0000ff>super</font>(s); }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> RethrowNew {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> f() <font color=#0000ff>throws</font> OneException {
    System.out.println(<font color=#004488>"originating the exception in f()"</font>);
    <font color=#0000ff>throw</font> <font color=#0000ff>new</font> OneException(<font color=#004488>"thrown from f()"</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
  main(String[] args) <font color=#0000ff>throws</font> TwoException {
    <font color=#0000ff>try</font> {
      f();
    } <font color=#0000ff>catch</font>(OneException e) {
      System.err.println(
        <font color=#004488>"Caught in main, e.printStackTrace()"</font>);
      e.printStackTrace();
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> TwoException(<font color=#004488>"from main()"</font>);
    }
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"originating the exception in f()"</font>,
      <font color=#004488>"Caught in main, e.printStackTrace()"</font>,
      <font color=#004488>"OneException: thrown from f()"</font>,
      <font color=#004488>"\tat RethrowNew.f(RethrowNew.java:18)"</font>,
      <font color=#004488>"\tat RethrowNew.main(RethrowNew.java:22)"</font>,
      <font color=#004488>"Exception in thread \"</font>main\<font color=#004488>" "</font> +
      <font color=#004488>"TwoException: from main()"</font>,
      <font color=#004488>"\tat RethrowNew.main(RethrowNew.java:28)"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The final exception knows only that it came from <b>main(&#160;)</b> and not from <b>f(&#160;)</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1555" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You never have to worry about cleaning up the previous exception, or any exceptions for that matter. They&#146;re all heap-based objects created with <b>new</b>, so the garbage collector automatically cleans them all up. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1556" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775707"></a><a name="Heading8705"></a>Exception chaining</h3>
<p>Often you want to catch one exception and throw another, but still keep the information about the originating exception&#151;this is called <i>exception chaining</i>. Prior to JDK 1.4, programmers had to write their own code to preserve the original exception information, but now all <b>Throwable</b> subclasses may take a <i>cause</i> object in their constructor. The <i>cause</i> is intended to be the originating exception, and by passing it in you maintain the stack trace back to its origin, even though you&#146;re creating and throwing a new exception at this point. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0509" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>It&#146;s interesting to note that the only <b>Throwable</b> subclasses that provide the <i>cause</i> argument in the constructor are the three fundamental exception classes <b>Error</b> (used by the JVM to report system errors), <b>Exception</b>, and <b>RuntimeException</b>. If you want to chain any other exception types, you do it through the <b>initCause(&#160;)</b> method rather than the constructor. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0510" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Here&#146;s an example that allows you to dynamically add fields to a <b>DynamicFields</b> object at run time:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:DynamicFields.java</font>
<font color=#009900>// A Class that dynamically adds fields to itself.</font>
<font color=#009900>// Demonstrates exception chaining.</font>
<font color=#009900>// {ThrowsException}</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> DynamicFieldsException <font color=#0000ff>extends</font> Exception {}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> DynamicFields {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>private</font> Object[][] fields;
  <font color=#0000ff>public</font> DynamicFields(<font color=#0000ff>int</font> initialSize) {
    fields = <font color=#0000ff>new</font> Object[initialSize][2];
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; initialSize; i++)
      fields[i] = <font color=#0000ff>new</font> Object[] { <font color=#0000ff>null</font>, <font color=#0000ff>null</font> };
  }
  <font color=#0000ff>public</font> String toString() {
    StringBuffer result = <font color=#0000ff>new</font> StringBuffer();
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; fields.length; i++) {
      result.append(fields[i][0]);
      result.append(<font color=#004488>": "</font>);
      result.append(fields[i][1]);
      result.append(<font color=#004488>"\n"</font>);
    }
    <font color=#0000ff>return</font> result.toString();

⌨️ 快捷键说明

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