<?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: Simple solution to resource collection</title>
	<atom:link href="http://chaoticjava.com/posts/simple-solution-to-resource-collection/feed/" rel="self" type="application/rss+xml" />
	<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/</link>
	<description>The internet, design patterns, frameworks and Java</description>
	<lastBuildDate>Sat, 05 Nov 2011 18:39:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: amira</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-76221</link>
		<dc:creator>amira</dc:creator>
		<pubDate>Mon, 05 Apr 2010 16:13:36 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-76221</guid>
		<description>findAverage = sum/no. of students
successRate = no. of passed / no. of students *100
Save them in the output file.</description>
		<content:encoded><![CDATA[<p>findAverage = sum/no. of students<br />
successRate = no. of passed / no. of students *100<br />
Save them in the output file.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aviad</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-60850</link>
		<dc:creator>Aviad</dc:creator>
		<pubDate>Fri, 21 Aug 2009 21:57:49 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-60850</guid>
		<description>@Chronos: Did you use some sort of generic method for closing Closeables? If so, which?</description>
		<content:encoded><![CDATA[<p>@Chronos: Did you use some sort of generic method for closing Closeables? If so, which?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chronos</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-60828</link>
		<dc:creator>Chronos</dc:creator>
		<pubDate>Fri, 21 Aug 2009 18:14:41 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-60828</guid>
		<description>I ended up using a bunch of wrappers to the JDBC classes, implementing the closeable Interface.</description>
		<content:encoded><![CDATA[<p>I ended up using a bunch of wrappers to the JDBC classes, implementing the closeable Interface.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aviad</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-59363</link>
		<dc:creator>Aviad</dc:creator>
		<pubDate>Wed, 05 Aug 2009 05:44:06 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-59363</guid>
		<description>@Guillaume: I agree. I don&#039;t know why it slipped in my mind in my answer.. :)

Anyway, since the &quot;exception swallowing&quot; came up a few times I added the discussion in the comments to the post itself as an update.

Thanks!</description>
		<content:encoded><![CDATA[<p>@Guillaume: I agree. I don&#8217;t know why it slipped in my mind in my answer.. <img src='http://chaoticjava.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Anyway, since the &#8220;exception swallowing&#8221; came up a few times I added the discussion in the comments to the post itself as an update.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guillaume Poirier</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-59338</link>
		<dc:creator>Guillaume Poirier</dc:creator>
		<pubDate>Tue, 04 Aug 2009 19:02:02 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-59338</guid>
		<description>The problem with throwing the exception from the close in the finally is that it can hide a more important exception thrown in the try block. If there&#039;s an exception both in the try block and the finally block, it&#039;s the one from the finally that is thrown.</description>
		<content:encoded><![CDATA[<p>The problem with throwing the exception from the close in the finally is that it can hide a more important exception thrown in the try block. If there&#8217;s an exception both in the try block and the finally block, it&#8217;s the one from the finally that is thrown.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aviad</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-59317</link>
		<dc:creator>Aviad</dc:creator>
		<pubDate>Tue, 04 Aug 2009 03:59:35 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-59317</guid>
		<description>@Jesse: To respond to the other part of your comment, I do agree I could&#039;ve rethrown the IOExceptions, and usually I do, except in the &lt;code&gt;finally&lt;/code&gt; clause since I want all the resources to close. I admit I didn&#039;t know of the &quot;multiple-finally&quot; technique, but you have to admit it&#039;s dead-ugly!</description>
		<content:encoded><![CDATA[<p>@Jesse: To respond to the other part of your comment, I do agree I could&#8217;ve rethrown the IOExceptions, and usually I do, except in the <code>finally</code> clause since I want all the resources to close. I admit I didn&#8217;t know of the &#8220;multiple-finally&#8221; technique, but you have to admit it&#8217;s dead-ugly!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aviad</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-59316</link>
		<dc:creator>Aviad</dc:creator>
		<pubDate>Tue, 04 Aug 2009 03:54:46 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-59316</guid>
		<description>@Jesse: Originally the AutoclosePool did gather the exceptions and threw the last IOException or last RuntimeException (it gathered both). 

However, I changed this for a certain reason. First, since it uses the PhantomReference trick, it means that within normal operations such as creating a new &lt;code&gt;AutoclosePool&lt;/code&gt;, &lt;code&gt;use()&lt;/code&gt;ing a resource or &lt;code&gt;close()&lt;/code&gt;ing a pool it would clean previously unclosed pools. This allows for the pool to be safely discarded if forgotten.
However, it also meant that &lt;code&gt;close()&lt;/code&gt; doesn&#039;t guarantee that the exceptions thrown are &lt;em&gt;your&lt;/em&gt; exceptions and not some other pool&#039;s exceptions. This could be mended of course, but because of the rest of the behavior, I thought it would be easier to just hide the exceptions altogether. 

To solve this, I did add an optional &lt;code&gt;ExceptionHandler&lt;/code&gt; which receives the exceptions when they occur; this way, whenever an exception does occur it would be notifying the application in the correct context (a handler&#039;s context). 

Do you think a different design is preferred?</description>
		<content:encoded><![CDATA[<p>@Jesse: Originally the AutoclosePool did gather the exceptions and threw the last IOException or last RuntimeException (it gathered both). </p>
<p>However, I changed this for a certain reason. First, since it uses the PhantomReference trick, it means that within normal operations such as creating a new <code>AutoclosePool</code>, <code>use()</code>ing a resource or <code>close()</code>ing a pool it would clean previously unclosed pools. This allows for the pool to be safely discarded if forgotten.<br />
However, it also meant that <code>close()</code> doesn&#8217;t guarantee that the exceptions thrown are <em>your</em> exceptions and not some other pool&#8217;s exceptions. This could be mended of course, but because of the rest of the behavior, I thought it would be easier to just hide the exceptions altogether. </p>
<p>To solve this, I did add an optional <code>ExceptionHandler</code> which receives the exceptions when they occur; this way, whenever an exception does occur it would be notifying the application in the correct context (a handler&#8217;s context). </p>
<p>Do you think a different design is preferred?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jesse Glick</title>
		<link>http://chaoticjava.com/posts/simple-solution-to-resource-collection/comment-page-1/#comment-59302</link>
		<dc:creator>Jesse Glick</dc:creator>
		<pubDate>Mon, 03 Aug 2009 22:37:56 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/?p=301#comment-59302</guid>
		<description>In the absence of something like AutoclosePool I would write the original method thusly:

void copy(String source, String target) throws IOException {
  InputStream is = new FileInputStream(source);
  try {
    OutputStream os = new FileOutputStream(target);
    try {
      // do the copying
    } finally {
      os.close();
    }
  } finally {
    is.close();
  }
}

Note the use of the nested finally blocks; this guarantees that everything always gets closed, without null checks. (It is even a bit safer, in case fis.close() threw some RuntimeException.) The downside is deeper indentation, I guess.

Note also that opening streams and working with them can usually throw IOException, so there is no need to catch it when closing. Better to have the exception be thrown and properly reported than ignored (meaning something might be very wrong and you don&#039;t know it). IMHO AutoclosePool.close() should be declared to throw IOException; it could collect any IOException&#039;s coming from the pool&#039;s members and either arbitrarily pick one to rethrow at the end, or (if you cared a lot about diagnostics) stack them together using Throwable.initCause.</description>
		<content:encoded><![CDATA[<p>In the absence of something like AutoclosePool I would write the original method thusly:</p>
<p>void copy(String source, String target) throws IOException {<br />
  InputStream is = new FileInputStream(source);<br />
  try {<br />
    OutputStream os = new FileOutputStream(target);<br />
    try {<br />
      // do the copying<br />
    } finally {<br />
      os.close();<br />
    }<br />
  } finally {<br />
    is.close();<br />
  }<br />
}</p>
<p>Note the use of the nested finally blocks; this guarantees that everything always gets closed, without null checks. (It is even a bit safer, in case fis.close() threw some RuntimeException.) The downside is deeper indentation, I guess.</p>
<p>Note also that opening streams and working with them can usually throw IOException, so there is no need to catch it when closing. Better to have the exception be thrown and properly reported than ignored (meaning something might be very wrong and you don&#8217;t know it). IMHO AutoclosePool.close() should be declared to throw IOException; it could collect any IOException&#8217;s coming from the pool&#8217;s members and either arbitrarily pick one to rethrow at the end, or (if you cared a lot about diagnostics) stack them together using Throwable.initCause.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

