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

📄 tij311.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    }
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Throw SimpleException from f()"</font>,
      <font color=#004488>"Caught it!"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The compiler creates a default constructor, which automatically (and invisibly) calls the base-class default constructor. Of course, in this case you don&#146;t get a <b>SimpleException(String)</b> constructor, but in practice that isn&#146;t used much. As you&#146;ll see, the most important thing about an exception is the class name, so most of the time an exception like the one shown here is satisfactory. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1532" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Here, the result is printed to the console <a name="Index800"></a><i>standard error</i> stream by writing to <a name="Index801"></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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1533" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can also create an exception class that has a constructor with a <b>String</b> argument:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:FullConstructors.java</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<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>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> 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();
    }
    <font color=#0000ff>try</font> {
      g();
    } <font color=#0000ff>catch</font>(MyException e) {
      e.printStackTrace();
    }
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Throwing MyException from f()"</font>,
      <font color=#004488>"MyException"</font>,
      <font color=#004488>"%% \tat FullConstructors.f\\(.*\\)"</font>,
      <font color=#004488>"%% \tat FullConstructors.main\\(.*\\)"</font>,
      <font color=#004488>"Throwing MyException from g()"</font>,
      <font color=#004488>"MyException: Originated in g()"</font>,
      <font color=#004488>"%% \tat FullConstructors.g\\(.*\\)"</font>,
      <font color=#004488>"%% \tat FullConstructors.main\\(.*\\)"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The added code is small: 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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1534" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In the handlers, one of the <b>Throwable</b> (from which <b>Exception</b> is inherited) methods is called: <b>printStackTrace(&#160;)</b>. This produces information about the sequence of methods that were called to get to the point where the exception happened. By default, the information goes to the standard error stream, but overloaded versions allow you to send the results to any other stream as well. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1535" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The process of creating your own exceptions can be taken further. You can add extra constructors and members:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:ExtraFeatures.java</font>
<font color=#009900>// Further embellishment of exception classes.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> MyException2 <font color=#0000ff>extends</font> Exception {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> x;
  <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);
    <font color=#0000ff>this</font>.x = x;
  }
  <font color=#0000ff>public</font> <font color=#0000ff>int</font> val() { <font color=#0000ff>return</font> x; }
  <font color=#0000ff>public</font> String getMessage() {
    <font color=#0000ff>return</font> <font color=#004488>"Detail Message: "</font>+ x + <font color=#004488>" "</font>+ <font color=#0000ff>super</font>.getMessage();
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ExtraFeatures {
  <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> 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();
    }
    <font color=#0000ff>try</font> {
      g();
    } <font color=#0000ff>catch</font>(MyException2 e) {
      e.printStackTrace();
    }
    <font color=#0000ff>try</font> {
      h();
    } <font color=#0000ff>catch</font>(MyException2 e) {
      e.printStackTrace();
      System.err.println(<font color=#004488>"e.val() = "</font> + e.val());
    }
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Throwing MyException2 from f()"</font>,
      <font color=#004488>"MyException2: Detail Message: 0 null"</font>,
      <font color=#004488>"%% \tat ExtraFeatures.f\\(.*\\)"</font>,
      <font color=#004488>"%% \tat ExtraFeatures.main\\(.*\\)"</font>,
      <font color=#004488>"Throwing MyException2 from g()"</font>,
      <font color=#004488>"MyException2: Detail Message: 0 Originated in g()"</font>,
      <font color=#004488>"%% \tat ExtraFeatures.g\\(.*\\)"</font>,
      <font color=#004488>"%% \tat ExtraFeatures.main\\(.*\\)"</font>,
      <font color=#004488>"Throwing MyException2 from h()"</font>,

⌨️ 快捷键说明

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