<?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: A Tale of Bad Exception Handling in Finally Blocks in Java</title>
	<atom:link href="http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 03:57:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: Basil Vandegriend</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-142500</link>
		<dc:creator>Basil Vandegriend</dc:creator>
		<pubDate>Thu, 17 Nov 2011 04:18:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-142500</guid>
		<description>@dayDreamer if #7 (caught) is referring to catching the exception raised in #6, then you are right in how it would work. If you are expecting #7 to catch the exception from #1, I don&#039;t believe it will, as exception #1 was not thrown within the scope of the #4/#5 try...catch block.

But it is easy to write a simple test and see what will happen, like I did in my article. No need to take my word for it :)</description>
		<content:encoded><![CDATA[<p>@dayDreamer if #7 (caught) is referring to catching the exception raised in #6, then you are right in how it would work. If you are expecting #7 to catch the exception from #1, I don&#8217;t believe it will, as exception #1 was not thrown within the scope of the #4/#5 try&#8230;catch block.</p>
<p>But it is easy to write a simple test and see what will happen, like I did in my article. No need to take my word for it :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dayDreamer</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-140894</link>
		<dc:creator>dayDreamer</dc:creator>
		<pubDate>Sat, 29 Oct 2011 18:53:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-140894</guid>
		<description>Hello,
I understood your  solution for Abhisheks question..but what is wrong in saying this...
Lets have a step-by-step execution.
1.RuntimeException thrown
2.no catch statement,so it is still waiting for someone to catch
3.finally encountered. (still exception not caught)
4.it has a try and catch block.  
5.catch(Exception e) waiting for an exception if any.
6.An uncaught exception found.(Maybe he found somewhere in buffer or so..)
7.caught!!
8.finish

At what point am i wrong?
Thanks.</description>
		<content:encoded><![CDATA[<p>Hello,<br />
I understood your  solution for Abhisheks question..but what is wrong in saying this&#8230;<br />
Lets have a step-by-step execution.<br />
1.RuntimeException thrown<br />
2.no catch statement,so it is still waiting for someone to catch<br />
3.finally encountered. (still exception not caught)<br />
4.it has a try and catch block.<br />
5.catch(Exception e) waiting for an exception if any.<br />
6.An uncaught exception found.(Maybe he found somewhere in buffer or so..)<br />
7.caught!!<br />
8.finish</p>
<p>At what point am i wrong?<br />
Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Basil Vandegriend</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-135576</link>
		<dc:creator>Basil Vandegriend</dc:creator>
		<pubDate>Sun, 31 Jul 2011 04:56:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-135576</guid>
		<description>Hi Abhishek.

I assume you want the try...catch block within the finally to catch the RuntimeException thrown by method2(). This won&#039;t work because the RuntimeException is thrown outside of that try..catch block. In other words, exceptions thrown from within a try...finally block are NOT re-thrown from within the finally block, and thus cannot be caught there.

The solution is to add a catch to the try...finally:
private void method1() {
  try {
    method2();
  } catch (Exception e) {
    System.out.println(&quot;Will catch exception from method2.&quot;);
  } finally {
    System.out.println(“Finally executes”);
  }
}

private void method2() {
  throw new RuntimeException(“Exception thrown from here.”);
}</description>
		<content:encoded><![CDATA[<p>Hi Abhishek.</p>
<p>I assume you want the try&#8230;catch block within the finally to catch the RuntimeException thrown by method2(). This won&#8217;t work because the RuntimeException is thrown outside of that try..catch block. In other words, exceptions thrown from within a try&#8230;finally block are NOT re-thrown from within the finally block, and thus cannot be caught there.</p>
<p>The solution is to add a catch to the try&#8230;finally:<br />
private void method1() {<br />
  try {<br />
    method2();<br />
  } catch (Exception e) {<br />
    System.out.println(&#8220;Will catch exception from method2.&#8221;);<br />
  } finally {<br />
    System.out.println(“Finally executes”);<br />
  }<br />
}</p>
<p>private void method2() {<br />
  throw new RuntimeException(“Exception thrown from here.”);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhishek Shrma</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-135523</link>
		<dc:creator>Abhishek Shrma</dc:creator>
		<pubDate>Sat, 30 Jul 2011 06:34:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-135523</guid>
		<description>Hi,
I am just doing one interesting thing... Instead of catching a exception properly .. I just put one more try/catch inside the finally clause and expecting that exception will catched be here and no abnormal termination will occur but that doesn&#039;t happen.
Code :

	private void method1() {
		try {
			method2();
		} finally {
			System.out.println(&quot;Finally executes&quot;);
			try {

			} catch (Exception e) {
				System.out.println(&quot;caught here in finally&quot;);
			}
		}
	}

	private void method2() {
			throw new RuntimeException(&quot;Exception thrown from here.&quot;);
	}
Can you please see over this??</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I am just doing one interesting thing&#8230; Instead of catching a exception properly .. I just put one more try/catch inside the finally clause and expecting that exception will catched be here and no abnormal termination will occur but that doesn&#8217;t happen.<br />
Code :</p>
<p>	private void method1() {<br />
		try {<br />
			method2();<br />
		} finally {<br />
			System.out.println(&#8220;Finally executes&#8221;);<br />
			try {</p>
<p>			} catch (Exception e) {<br />
				System.out.println(&#8220;caught here in finally&#8221;);<br />
			}<br />
		}<br />
	}</p>
<p>	private void method2() {<br />
			throw new RuntimeException(&#8220;Exception thrown from here.&#8221;);<br />
	}<br />
Can you please see over this??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Basil Vandegriend</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-130207</link>
		<dc:creator>Basil Vandegriend</dc:creator>
		<pubDate>Fri, 13 May 2011 04:14:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-130207</guid>
		<description>@chaumant, it sounds like in your catch blocks you do not want to re-throw an exception - just send an email. In this case execution will continue after the catch blocks, so you could just eliminate the finally block and have the cleanup code afterwards.</description>
		<content:encoded><![CDATA[<p>@chaumant, it sounds like in your catch blocks you do not want to re-throw an exception &#8211; just send an email. In this case execution will continue after the catch blocks, so you could just eliminate the finally block and have the cleanup code afterwards.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chaumant</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-130168</link>
		<dc:creator>chaumant</dc:creator>
		<pubDate>Thu, 12 May 2011 06:29:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-130168</guid>
		<description>No doubt, Its a good and helpful article.
But it leaves certain questions to me.
I am in the process of developing a code, which actually process set of teams. I wanted to continue if exception is thrown while processing Teams.
And the moment it throws exception, it the process should send a mail for that team.
for(team : Teams){
try{
//process Teams
}
catch(SomeException se){
//Send mail if exception comes
}
catch(Exception e){
//Send mail if exception comes
}
finally{
//cleanup code for the team
continue;  // Ofcourse this line is giving waring - &quot;Finally block does not complete normally&quot; and i am also not interested in any warning
}
}

Please advise..what could be alternatives.</description>
		<content:encoded><![CDATA[<p>No doubt, Its a good and helpful article.<br />
But it leaves certain questions to me.<br />
I am in the process of developing a code, which actually process set of teams. I wanted to continue if exception is thrown while processing Teams.<br />
And the moment it throws exception, it the process should send a mail for that team.<br />
for(team : Teams){<br />
try{<br />
//process Teams<br />
}<br />
catch(SomeException se){<br />
//Send mail if exception comes<br />
}<br />
catch(Exception e){<br />
//Send mail if exception comes<br />
}<br />
finally{<br />
//cleanup code for the team<br />
continue;  // Ofcourse this line is giving waring &#8211; &#8220;Finally block does not complete normally&#8221; and i am also not interested in any warning<br />
}<br />
}</p>
<p>Please advise..what could be alternatives.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Basil Vandegriend</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-128371</link>
		<dc:creator>Basil Vandegriend</dc:creator>
		<pubDate>Sun, 17 Apr 2011 20:57:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-128371</guid>
		<description>@Student, you can do that by returning a special error code from a catch block. This clearly signals what is going on. Just don&#039;t put a return statement in the finally block.</description>
		<content:encoded><![CDATA[<p>@Student, you can do that by returning a special error code from a catch block. This clearly signals what is going on. Just don&#8217;t put a return statement in the finally block.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Student</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-128145</link>
		<dc:creator>Student</dc:creator>
		<pubDate>Thu, 14 Apr 2011 22:21:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-128145</guid>
		<description>So what is your recommended implementation if the caller of performBusinessOperation() expects a valid return value that only performBusinessOperation() can return. For example, an error code.</description>
		<content:encoded><![CDATA[<p>So what is your recommended implementation if the caller of performBusinessOperation() expects a valid return value that only performBusinessOperation() can return. For example, an error code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arao</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-106299</link>
		<dc:creator>Arao</dc:creator>
		<pubDate>Thu, 08 Apr 2010 20:35:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-106299</guid>
		<description>do not u guys read Practical Java??</description>
		<content:encoded><![CDATA[<p>do not u guys read Practical Java??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geza</title>
		<link>http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java/comment-page-1#comment-105766</link>
		<dc:creator>Geza</dc:creator>
		<pubDate>Tue, 23 Mar 2010 12:10:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java#comment-105766</guid>
		<description>Hmmm. The problem in my case was that I used return in the finally block, after I removed that outside, the RuntimeException is propagated appropriately.

Conslusion: do not use return statement in finally block, it can ruin your exception propagation or return policy.</description>
		<content:encoded><![CDATA[<p>Hmmm. The problem in my case was that I used return in the finally block, after I removed that outside, the RuntimeException is propagated appropriately.</p>
<p>Conslusion: do not use return statement in finally block, it can ruin your exception propagation or return policy.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

