<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Tag close</title>
    <link>http://blog.objectmentor.com/articles/tag/close</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Always &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; in a &amp;lt;tt&amp;gt;finally&amp;lt;/tt&amp;gt; block</title>
      <description>&lt;p&gt;Here&amp;#8217;s one for my fellow Java programmers, but it&amp;#8217;s really generally applicable.&lt;/p&gt;


	&lt;p&gt;When you call &lt;tt&gt;close()&lt;/tt&gt; on I/O streams, readers, writers, network sockets, database connections, &lt;em&gt;etc.&lt;/em&gt;, it&amp;#8217;s easy to forgot the most appropriate idiom. I just spent a few hours fixing some examples of misuse in otherwise very good Java code.&lt;/p&gt;


	&lt;p&gt;What&amp;#8217;s wrong the following code?&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public void writeContentToFile(String content, String fileName) throws Exception {
    File output = new File(fileName);
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(output), "UTF-8");
    writer.write(content);
    writer.close();
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;It doesn&amp;#8217;t look all that bad. It tells it&amp;#8217;s story. It&amp;#8217;s easy to understand.&lt;/p&gt;


	&lt;p&gt;However, &lt;em&gt;it&amp;#8217;s quite likely&lt;/em&gt; that you won&amp;#8217;t get to the last line, which closes the &lt;tt&gt;writer&lt;/tt&gt;, from time to time. File and network I/O errors are common. For example, what if you can&amp;#8217;t actually write to the location specified by &lt;tt&gt;fileName&lt;/tt&gt;? So, we have to be more defensive. We want to be sure we always clean up.&lt;/p&gt;


	&lt;p&gt;The correct idiom is to use a &lt;tt&gt;try &amp;#8230; finally &amp;#8230;&lt;/tt&gt; block.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public void writeContentToFile(String content, String fileName) throws Exception {
    File output = new File(getFileSystemPath() + contentFilename);
    OutputStreamWriter writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(output), "UTF-8");
        writer.write(content);
    } finally {
        if (writer != null)
            writer.close();
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now, no matter what happens, the writer will be closed, if it&amp;#8217;s not null, even if writing the output was unsuccessful.&lt;/p&gt;


	&lt;p&gt;Note that we don&amp;#8217;t necessarily need a &lt;tt&gt;catch&lt;/tt&gt; block, because in this case we&amp;#8217;re willing to let any &lt;tt&gt;Exception&lt;/tt&gt;s propagate up the stack (notice the throws clause). A lot of developers don&amp;#8217;t realize that there are times when you need a &lt;tt&gt;try&lt;/tt&gt; block, but not necessarily a &lt;tt&gt;catch&lt;/tt&gt; block. This is one of those times.&lt;/p&gt;


	&lt;p&gt;So, anytime you need to clean up or otherwise release resources, use a &lt;tt&gt;finally&lt;/tt&gt; block to ensure that the clean up happens, no matter what.&lt;/p&gt;</description>
      <pubDate>Thu, 31 Jul 2008 00:12:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2eda3563-e87a-4c15-a93d-f842f8de8f68</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/07/31/always-close-in-a-finally-block</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>close</category>
      <category>finally</category>
      <category>Java</category>
    </item>
  </channel>
</rss>
