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

📄 tij0103.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0102.html">Prev</a> | <a href="tij0104.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Constructors<P><A NAME="Index960"></A><A NAME="Index961"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
writing code with exceptions, it&#8217;s particularly important that you always
ask, &#8220;If an exception occurs, will this be properly cleaned up?&#8221;
Most of the time you&#8217;re fairly safe, but in constructors there&#8217;s a
problem. The constructor puts the object into a safe starting state, but it
might perform some operation &#8211; such as opening a file &#8211; that
doesn&#8217;t get cleaned up until the user is finished with the object and
calls a special cleanup method. If you throw an exception from inside a
constructor, these cleanup behaviors might not occur properly. This means that
you must be especially diligent while you write your constructor.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
you&#8217;ve just learned about <A NAME="Index962"></A><A NAME="Index963"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finally</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you might think that it is the correct solution. But it&#8217;s not quite that
simple, because 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finally
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">performs
the cleanup code 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>every
time
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
even in the situations in which you don&#8217;t want the cleanup code executed
until the cleanup method runs. Thus, if you do perform cleanup in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finally</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you must set some kind of flag when the constructor finishes normally and
don&#8217;t do anything in the finally block if the flag is set. Because this
isn&#8217;t particularly elegant (you are coupling your code from one place to
another), it&#8217;s best if you try to avoid performing this kind of cleanup in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finally</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
unless you are forced to.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the following example, a class called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created that opens a file and allows you to read it one line (converted into
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
at a time. It uses the classes <A NAME="Index964"></A><A NAME="Index965"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileReader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index966"></A><A NAME="Index967"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BufferedReader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
from the Java standard IO library that will be discussed in Chapter 10, but
which are simple enough that you probably won&#8217;t have any trouble
understanding their basic use:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Cleanup.java</font>
<font color="#009900">// Paying attention to exceptions</font>
<font color="#009900">// in constructors</font>
<font color="#0000ff">import</font> java.io.*;

<font color="#0000ff">class</font> InputFile {
  <font color="#0000ff">private</font> BufferedReader in;
  InputFile(String fname) <font color="#0000ff">throws</font> Exception {
    <font color="#0000ff">try</font> {
      in = 
        <font color="#0000ff">new</font> BufferedReader(
          <font color="#0000ff">new</font> FileReader(fname));
      <font color="#009900">// Other code that might throw exceptions</font>
    } <font color="#0000ff">catch</font>(FileNotFoundException e) {
      System.out.println(
        "Could not open " + fname);
      <font color="#009900">// Wasn't open, so don't close it</font>
      <font color="#0000ff">throw</font> e;
    } <font color="#0000ff">catch</font>(Exception e) {
      <font color="#009900">// All other exceptions must close it</font>
      <font color="#0000ff">try</font> {
        in.close();
      } <font color="#0000ff">catch</font>(IOException e2) {
        System.out.println(
          "in.close() unsuccessful");
      }
      <font color="#0000ff">throw</font> e;
    } <font color="#0000ff">finally</font> {
      <font color="#009900">// Don't close it here!!!</font>
    }
  }
  String getLine() {
    String s;
    <font color="#0000ff">try</font> {
      s = in.readLine();
    } <font color="#0000ff">catch</font>(IOException e) {
      System.out.println(
        "readLine() unsuccessful");
      s = "failed";
    }
    <font color="#0000ff">return</font> s;
  }
  <font color="#0000ff">void</font> cleanup() {
    <font color="#0000ff">try</font> {
      in.close();
    } <font color="#0000ff">catch</font>(IOException e2) {
      System.out.println(
        "in.close() unsuccessful");
    }
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> Cleanup {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    <font color="#0000ff">try</font> {
      InputFile in = 
        <font color="#0000ff">new</font> InputFile("Cleanup.java");
      String s;
      <font color="#0000ff">int</font> i = 1;
      <font color="#0000ff">while</font>((s = in.getLine()) != <font color="#0000ff">null</font>)
        System.out.println(""+ i++ + ": " + s);
      in.cleanup();
    } <font color="#0000ff">catch</font>(Exception e) {
      System.out.println(
        "Caught in main, 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">This
example uses Java 1.1<A NAME="Index968"></A>
IO classes.

⌨️ 快捷键说明

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