<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Please close the resource behind you.</title>
	<atom:link href="http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 08 Feb 2012 14:41:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Xebia Blog</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18818</link>
		<dc:creator>Xebia Blog</dc:creator>
		<pubDate>Wed, 03 Oct 2007 12:57:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18818</guid>
		<description>[...] In a previous blog I described some ways to make sure your resources are closed properly. The examples I gave contained a bug and may cause that resources are still not properly closed (as pointed out by Anonymous and my colleague here on my project). Here is what was wrong with it.. [...]</description>
		<content:encoded><![CDATA[<p>[...] In a previous blog I described some ways to make sure your resources are closed properly. The examples I gave contained a bug and may cause that resources are still not properly closed (as pointed out by Anonymous and my colleague here on my project). Here is what was wrong with it.. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars Vonk</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18745</link>
		<dc:creator>Lars Vonk</dc:creator>
		<pubDate>Tue, 02 Oct 2007 18:36:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18745</guid>
		<description>I see your point. If closing fails it can be possible that not everything is written to the file. So instead of closing it in finally it should be closed in the catch blocks. Here is another version of the template class:

public class CloseableTemplate&lt;T&gt; {
..public void execute(CloseableCallbackHandler&lt;T&gt; callbackHandler, T closeable) {
....try {
......callbackHandler.doWithCloseable(closeable);
......// close reader normally, we are interested whether or not this fails.
......reader.close();
....} catch(Exception e) {
......close(closeable);
......// rethrow as runtime
......throw new CustomRuntimeException(e);
....} 
..}
..private void close(Closeable closeable) {
....try {
......if(closeable != null) {
........closeable.close();
......}
....} catch(IOException e) {
......log.info(\\\&quot;closing reader failed but since there is another exception that occurred first we only log this one.\\\&quot;, e);
....}
..}
}
</description>
		<content:encoded><![CDATA[<p>I see your point. If closing fails it can be possible that not everything is written to the file. So instead of closing it in finally it should be closed in the catch blocks. Here is another version of the template class:</p>
<p>public class CloseableTemplate<t> {<br />
..public void execute(CloseableCallbackHandler</t><t> callbackHandler, T closeable) {<br />
&#8230;.try {<br />
&#8230;&#8230;callbackHandler.doWithCloseable(closeable);<br />
&#8230;&#8230;// close reader normally, we are interested whether or not this fails.<br />
&#8230;&#8230;reader.close();<br />
&#8230;.} catch(Exception e) {<br />
&#8230;&#8230;close(closeable);<br />
&#8230;&#8230;// rethrow as runtime<br />
&#8230;&#8230;throw new CustomRuntimeException(e);<br />
&#8230;.}<br />
..}<br />
..private void close(Closeable closeable) {<br />
&#8230;.try {<br />
&#8230;&#8230;if(closeable != null) {<br />
&#8230;&#8230;..closeable.close();<br />
&#8230;&#8230;}<br />
&#8230;.} catch(IOException e) {<br />
&#8230;&#8230;log.info(\\\&#8221;closing reader failed but since there is another exception that occurred first we only log this one.\\\&#8221;, e);<br />
&#8230;.}<br />
..}<br />
}</t></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18544</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Sat, 29 Sep 2007 12:04:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18544</guid>
		<description>Hey, why haven&#039;t you fixed YOUR BUGGY CODE yet?

public void copyFile() throws IOException {
..BufferedReader reader = null;
..try {
....reader = new BufferedReader(new FileReader(&quot;foo.txt&quot;));
....String line = null;
....while((line = reader.readLine()) != null) {
......String formattedLine = Parser.parseLine(line);
....}
....// everything fine here: NO exception!
..} finally {
....try {
......// now reader.close() throws an exception
......reader.close();
....} catch(IOException e) {
......log.info(&quot;closing reader failed and my caller will never know!&quot;, e);
....}
..}
// return normally without any exception
}</description>
		<content:encoded><![CDATA[<p>Hey, why haven&#8217;t you fixed YOUR BUGGY CODE yet?</p>
<p>public void copyFile() throws IOException {<br />
..BufferedReader reader = null;<br />
..try {<br />
&#8230;.reader = new BufferedReader(new FileReader(&#8220;foo.txt&#8221;));<br />
&#8230;.String line = null;<br />
&#8230;.while((line = reader.readLine()) != null) {<br />
&#8230;&#8230;String formattedLine = Parser.parseLine(line);<br />
&#8230;.}<br />
&#8230;.// everything fine here: NO exception!<br />
..} finally {<br />
&#8230;.try {<br />
&#8230;&#8230;// now reader.close() throws an exception<br />
&#8230;&#8230;reader.close();<br />
&#8230;.} catch(IOException e) {<br />
&#8230;&#8230;log.info(&#8220;closing reader failed and my caller will never know!&#8221;, e);<br />
&#8230;.}<br />
..}<br />
// return normally without any exception<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars Vonk</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18229</link>
		<dc:creator>Lars Vonk</dc:creator>
		<pubDate>Wed, 26 Sep 2007 06:55:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18229</guid>
		<description>Hi Anonymous,

Thanks for your comments. 

@1: I don&#039;t consider logging an Exception the same as swallowing. I consider swallowing to be empty catch blocks and in case you &quot;miss&quot; an exception due to the reason I described in my blog.
Like I said maybe logging on info level is not the best option but at least it will be looked at by the application maintainer (at least you hope ;-)). 
Personally I would not propagate the IOException that occurred for closing a resource all the way up to the end user. What can he do about it? 

@2: true that&#039;s why I guess you need solid finally blocks.

@3: true, but I hope I showed that with little effort or even less effort in case of the Template approach  you can save yourself a lot of problems in those &quot;hardly&quot; cases. 
(It is less because you only have to create the Template once and can use that throughout you application(s)).</description>
		<content:encoded><![CDATA[<p>Hi Anonymous,</p>
<p>Thanks for your comments. </p>
<p>@1: I don&#8217;t consider logging an Exception the same as swallowing. I consider swallowing to be empty catch blocks and in case you &#8220;miss&#8221; an exception due to the reason I described in my blog.<br />
Like I said maybe logging on info level is not the best option but at least it will be looked at by the application maintainer (at least you hope <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ).<br />
Personally I would not propagate the IOException that occurred for closing a resource all the way up to the end user. What can he do about it? </p>
<p>@2: true that&#8217;s why I guess you need solid finally blocks.</p>
<p>@3: true, but I hope I showed that with little effort or even less effort in case of the Template approach  you can save yourself a lot of problems in those &#8220;hardly&#8221; cases.<br />
(It is less because you only have to create the Template once and can use that throughout you application(s)).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18190</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 25 Sep 2007 21:32:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18190</guid>
		<description>Hello again!

1. Your code has a severe bug. Consider: Your function succeeds, only reader.close() throws an exception. Then

finally {
..try {
....reader.close();
..} catch(IOException e) {
....log.info(&quot;closing reader failed.&quot;, e);
..} 
} 

simply swallows the exception from reader.close(). This is tolerable for a Reader but certainly not in general e.g. for a Writer and other &#039;resources&#039;.

2. It is very hard to maintain the original exception when another exception occurs during stack unwinding.

3. &#039;Hidden&#039; exceptions are a mere theoretical problem. They are hardly ever seen in real world applications and it makes no sense to add extra code to handle them.</description>
		<content:encoded><![CDATA[<p>Hello again!</p>
<p>1. Your code has a severe bug. Consider: Your function succeeds, only reader.close() throws an exception. Then</p>
<p>finally {<br />
..try {<br />
&#8230;.reader.close();<br />
..} catch(IOException e) {<br />
&#8230;.log.info(&#8220;closing reader failed.&#8221;, e);<br />
..}<br />
} </p>
<p>simply swallows the exception from reader.close(). This is tolerable for a Reader but certainly not in general e.g. for a Writer and other &#8216;resources&#8217;.</p>
<p>2. It is very hard to maintain the original exception when another exception occurs during stack unwinding.</p>
<p>3. &#8216;Hidden&#8217; exceptions are a mere theoretical problem. They are hardly ever seen in real world applications and it makes no sense to add extra code to handle them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars Vonk</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18138</link>
		<dc:creator>Lars Vonk</dc:creator>
		<pubDate>Tue, 25 Sep 2007 12:32:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18138</guid>
		<description>Hi Gert-Jan, not much of an improvement compared to what? I am guessing you are referring to the Template-Callback approach versus the call to the close method in the finally block. I agree with you that the inline Template-Callback approach is not really readable but it does guarantee that the resources will be closed (you don&#039;t have to worry about the try-finally anymore). Maybe if you would create a separate class instead of the inlining it would be more readable...</description>
		<content:encoded><![CDATA[<p>Hi Gert-Jan, not much of an improvement compared to what? I am guessing you are referring to the Template-Callback approach versus the call to the close method in the finally block. I agree with you that the inline Template-Callback approach is not really readable but it does guarantee that the resources will be closed (you don&#8217;t have to worry about the try-finally anymore). Maybe if you would create a separate class instead of the inlining it would be more readable&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gert-Jan van der Heiden</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18136</link>
		<dc:creator>Gert-Jan van der Heiden</dc:creator>
		<pubDate>Tue, 25 Sep 2007 12:18:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18136</guid>
		<description>I dont think it is much of an improvement. The amount of code isn&#039;t drastically changed. Also, IMHO, the code isn&#039;t easy to read.

In the finally block, the reader should be checked for not null.</description>
		<content:encoded><![CDATA[<p>I dont think it is much of an improvement. The amount of code isn&#8217;t drastically changed. Also, IMHO, the code isn&#8217;t easy to read.</p>
<p>In the finally block, the reader should be checked for not null.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gert-Jan van der Heiden</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18135</link>
		<dc:creator>Gert-Jan van der Heiden</dc:creator>
		<pubDate>Tue, 25 Sep 2007 12:11:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18135</guid>
		<description>Not much of an improvement. About the same amount of code, only the solution is not really readable.</description>
		<content:encoded><![CDATA[<p>Not much of an improvement. About the same amount of code, only the solution is not really readable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars Vonk</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18131</link>
		<dc:creator>Lars Vonk</dc:creator>
		<pubDate>Tue, 25 Sep 2007 11:38:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18131</guid>
		<description>@Anonymous: You could of course log it on any log level. Or check it there is an exception thrown or not and depending on that throw the exception that occurred on the close method.
But not handling the Exception like in my first example is worse as you are then not even aware of the original exception.</description>
		<content:encoded><![CDATA[<p>@Anonymous: You could of course log it on any log level. Or check it there is an exception thrown or not and depending on that throw the exception that occurred on the close method.<br />
But not handling the Exception like in my first example is worse as you are then not even aware of the original exception.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18108</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 25 Sep 2007 08:04:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/#comment-18108</guid>
		<description>log.info(&quot;...&quot;, e);
isn&#039;t really an option. It&#039;s not better than swallowing the exception.</description>
		<content:encoded><![CDATA[<p>log.info(&#8220;&#8230;&#8221;, e);<br />
isn&#8217;t really an option. It&#8217;s not better than swallowing the exception.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/2007/09/24/please-close-the-resource-behind-you/feed/ ) in 0.52862 seconds, on Feb 9th, 2012 at 8:08 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 9:08 pm UTC -->
