<?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: Finally finally (block)</title>
	<atom:link href="http://chaoticjava.com/posts/finally-finally-block/feed/" rel="self" type="application/rss+xml" />
	<link>http://chaoticjava.com/posts/finally-finally-block/</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.2.1</generator>
	<item>
		<title>By: sud</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-69175</link>
		<dc:creator>sud</dc:creator>
		<pubDate>Sat, 14 Nov 2009 15:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-69175</guid>
		<description>Great explanation of the behavior of the finally block in your comment from January 9th, 2008 at 7:06 am. I&#039;m reading Effective Java and though it&#039;s is great book, I wish the author had used similar plain English to explain the techniques and concepts.</description>
		<content:encoded><![CDATA[<p>Great explanation of the behavior of the finally block in your comment from January 9th, 2008 at 7:06 am. I&#8217;m reading Effective Java and though it&#8217;s is great book, I wish the author had used similar plain English to explain the techniques and concepts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simple solution to resource collection</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-59362</link>
		<dc:creator>Simple solution to resource collection</dc:creator>
		<pubDate>Wed, 05 Aug 2009 05:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-59362</guid>
		<description>[...] as pretty, but the resources are closed at the end of it! Notice the usage of finally to ensure that the resources are closed regardless of thrown exceptions. Also notice how each close [...]</description>
		<content:encoded><![CDATA[<p>[...] as pretty, but the resources are closed at the end of it! Notice the usage of finally to ensure that the resources are closed regardless of thrown exceptions. Also notice how each close [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neeti</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-50070</link>
		<dc:creator>Neeti</dc:creator>
		<pubDate>Fri, 01 May 2009 12:19:48 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-50070</guid>
		<description>a good explanation of finally block.</description>
		<content:encoded><![CDATA[<p>a good explanation of finally block.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-20985</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Wed, 09 Jan 2008 20:46:46 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-20985</guid>
		<description>I&#039;m happy it was made more clear for you.

Thanks again for your comments!</description>
		<content:encoded><![CDATA[<p>I&#8217;m happy it was made more clear for you.</p>
<p>Thanks again for your comments!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AndrÃ© Dietisheim</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-20982</link>
		<dc:creator>AndrÃ© Dietisheim</dc:creator>
		<pubDate>Wed, 09 Jan 2008 20:29:18 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-20982</guid>
		<description>thanks a lot for your comments! Reading it and thinking about it takes me to the opinion that it makes perfect sense. The key is

&gt; However, if you decide to return a value or throw a different exception
&gt; within the finally block, that would override the exception or return value
&gt; in the try block.

any return value or exception returned within a finally block overrides any return value or exception (in all 4 permutations) a try block would return.</description>
		<content:encoded><![CDATA[<p>thanks a lot for your comments! Reading it and thinking about it takes me to the opinion that it makes perfect sense. The key is</p>
<p>&gt; However, if you decide to return a value or throw a different exception<br />
&gt; within the finally block, that would override the exception or return value<br />
&gt; in the try block.</p>
<p>any return value or exception returned within a finally block overrides any return value or exception (in all 4 permutations) a try block would return.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-20910</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Wed, 09 Jan 2008 05:06:04 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-20910</guid>
		<description>Andre:

The behavior you showed is reasonable. What happens is this:
When you throw an exception, the finally block is called. When the finally block finishes, the exception continues on normally, so the finally block is only for closing resources etc.
&lt;b&gt;However&lt;/b&gt;, if you decide to return a value or throw a different exception &lt;i&gt;within the finally block&lt;/i&gt;, that would override the exception or return value in the try block.
In your &quot;aMethod&quot;, your try block threw a typed exception, and was later overridden by the finally block because it returned a value. The reason the compiler didn&#039;t request you to define the exception is because it identified that there is no chance for the exception to ever be actually thrown (the compiler is sometimes actually smart :))
In your main method, the value was returned (&quot;true&quot;) and not the exception, and therefore the &quot;not catched&quot; response was given. If you remove the &quot;return true&quot; from your finally block, and place something else (some assignment or whatever) you&#039;d see that (a) the compiler will request that you define the exception and (b) you&#039;d get the &quot;catched&quot; response.

Hope I was clear in this explanation..</description>
		<content:encoded><![CDATA[<p>Andre:</p>
<p>The behavior you showed is reasonable. What happens is this:<br />
When you throw an exception, the finally block is called. When the finally block finishes, the exception continues on normally, so the finally block is only for closing resources etc.<br />
<b>However</b>, if you decide to return a value or throw a different exception <i>within the finally block</i>, that would override the exception or return value in the try block.<br />
In your &#8220;aMethod&#8221;, your try block threw a typed exception, and was later overridden by the finally block because it returned a value. The reason the compiler didn&#8217;t request you to define the exception is because it identified that there is no chance for the exception to ever be actually thrown (the compiler is sometimes actually smart <img src='http://chaoticjava.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )<br />
In your main method, the value was returned (&#8220;true&#8221;) and not the exception, and therefore the &#8220;not catched&#8221; response was given. If you remove the &#8220;return true&#8221; from your finally block, and place something else (some assignment or whatever) you&#8217;d see that (a) the compiler will request that you define the exception and (b) you&#8217;d get the &#8220;catched&#8221; response.</p>
<p>Hope I was clear in this explanation..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AndrÃ© Dietisheim</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-20883</link>
		<dc:creator>AndrÃ© Dietisheim</dc:creator>
		<pubDate>Tue, 08 Jan 2008 20:57:46 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-20883</guid>
		<description>beside being bad practice (return value in a finally block) the following snippet shows a behavior I didn&#039;t know until shortly:

public class Test {

    private static boolean aMethod() {
	try {
	    throw new Exception();
	} finally {
	    return true;
	}
    }

    public static void main(String[] args) {
	try {
	    aMethod();
	    System.err.println(&quot;not catched!&quot;);
	} catch (Exception e) {
	    System.err.println(&quot;catched!&quot;);
	}
    }
}

To my old understanding (sorry for my ignorance) this snippet shouldn&#039;t have compiled because an Exception is thrown without any catching nor declaration in the method signature. 

1.) there&#039;s no need for a catch if there&#039;s a finally

But the other thing that astonished me is that the output is &quot;not catched&quot;. Looks like the exception is swallowed! 

2.) deliberately omitting a catch is like writing an empty catch block...

or did I miss something?</description>
		<content:encoded><![CDATA[<p>beside being bad practice (return value in a finally block) the following snippet shows a behavior I didn&#8217;t know until shortly:</p>
<p>public class Test {</p>
<p>    private static boolean aMethod() {<br />
	try {<br />
	    throw new Exception();<br />
	} finally {<br />
	    return true;<br />
	}<br />
    }</p>
<p>    public static void main(String[] args) {<br />
	try {<br />
	    aMethod();<br />
	    System.err.println(&#8220;not catched!&#8221;);<br />
	} catch (Exception e) {<br />
	    System.err.println(&#8220;catched!&#8221;);<br />
	}<br />
    }<br />
}</p>
<p>To my old understanding (sorry for my ignorance) this snippet shouldn&#8217;t have compiled because an Exception is thrown without any catching nor declaration in the method signature. </p>
<p>1.) there&#8217;s no need for a catch if there&#8217;s a finally</p>
<p>But the other thing that astonished me is that the output is &#8220;not catched&#8221;. Looks like the exception is swallowed! </p>
<p>2.) deliberately omitting a catch is like writing an empty catch block&#8230;</p>
<p>or did I miss something?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-20605</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Sat, 05 Jan 2008 07:42:55 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-20605</guid>
		<description>khandes3, you&#039;re correct. But is it a problem, considering that all (or at least most) resources are being cleared by the VM itself when it exits?

It&#039;s true though that the ones that are not need to be released by the exit hooks, though.</description>
		<content:encoded><![CDATA[<p>khandes3, you&#8217;re correct. But is it a problem, considering that all (or at least most) resources are being cleared by the VM itself when it exits?</p>
<p>It&#8217;s true though that the ones that are not need to be released by the exit hooks, though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: khandes3</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-20576</link>
		<dc:creator>khandes3</dc:creator>
		<pubDate>Fri, 04 Jan 2008 23:07:16 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-20576</guid>
		<description>Since the finally block occurs no matter what happened during the try block...

Actually, this is not always the case.
The finally block is only executed provided you don&#039;t call something like System.exit() in the try block. If you run such a program, you will find that the finally block is not executed.

 public class Test {
   public static void main(String [] args) {
     try {
       System.out.println(&quot;Message 1&quot;);
       System.exit(0);
     } finally {
       System.out.println(&quot;This line is not executed!&quot;);
     }
   }
 }</description>
		<content:encoded><![CDATA[<p>Since the finally block occurs no matter what happened during the try block&#8230;</p>
<p>Actually, this is not always the case.<br />
The finally block is only executed provided you don&#8217;t call something like System.exit() in the try block. If you run such a program, you will find that the finally block is not executed.</p>
<p> public class Test {<br />
   public static void main(String [] args) {<br />
     try {<br />
       System.out.println(&#8220;Message 1&#8243;);<br />
       System.exit(0);<br />
     } finally {<br />
       System.out.println(&#8220;This line is not executed!&#8221;);<br />
     }<br />
   }<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/finally-finally-block/comment-page-1/#comment-264</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Fri, 17 Feb 2006 23:02:34 +0000</pubDate>
		<guid isPermaLink="false">http://javachaos.crazyredpanda.com/?p=132#comment-264</guid>
		<description>That&#039;s actually very interesting and sounds likely - I didn&#039;t think Apple changed the way they compile into bytecode. 

Where do I get more information to this? The best I can get is the VM spec book, but it&#039;s certainly outdated..</description>
		<content:encoded><![CDATA[<p>That&#8217;s actually very interesting and sounds likely &#8211; I didn&#8217;t think Apple changed the way they compile into bytecode. </p>
<p>Where do I get more information to this? The best I can get is the VM spec book, but it&#8217;s certainly outdated..</p>
]]></content:encoded>
	</item>
</channel>
</rss>

