<?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: Why do catch clauses need to be ordered?</title>
	<atom:link href="http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/</link>
	<description>Thoughts on Java™, Java EE™ and web application development</description>
	<lastBuildDate>Fri, 30 Apr 2010 07:48:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: rajiv</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-62</link>
		<dc:creator>rajiv</dc:creator>
		<pubDate>Fri, 06 Jul 2007 21:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-62</guid>
		<description>Doug:&lt;br/&gt;Isn&#039;t that exactly what Sachin was saying? That overloaded methods are resolved at compile time and catch clauses are resolved at runtime?&lt;br/&gt;&lt;br/&gt;What I am expecting is that compiler could sort the catch clauses and put then in the class file in sorted fashion based on the class hierarchy at compile time. However, this need not affect the runtime resolution of catch clauses. That is, the catch clause executed at runtime is chosen based on the runtime type of the exception being raised.</description>
		<content:encoded><![CDATA[<p>Doug:<br />Isn&#8217;t that exactly what Sachin was saying? That overloaded methods are resolved at compile time and catch clauses are resolved at runtime?</p>
<p>What I am expecting is that compiler could sort the catch clauses and put then in the class file in sorted fashion based on the class hierarchy at compile time. However, this need not affect the runtime resolution of catch clauses. That is, the catch clause executed at runtime is chosen based on the runtime type of the exception being raised.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-61</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Mon, 02 Jul 2007 06:27:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-61</guid>
		<description>No, they (exceptions and overloading) aren&#039;t comparable. And yet they are comparable.&lt;br/&gt;&lt;br/&gt;They &lt;i&gt;aren&#039;t&lt;/i&gt; comparable because overloading results in a runtime reference to a method with a &lt;i&gt;specific&lt;/i&gt; signature. Exceptions, on the other hand, are not specific&#8212;a single &quot;catch&quot; clause can catch many subtypes of exceptions.&lt;br/&gt;&lt;br/&gt;They &lt;i&gt;are&lt;/i&gt; comparable because with both exceptions and overloading, the Java specifications leave it to the programmer to deal with the potential mismatch between compile-time and run-time. The proposal would take that control out of the hands of the programmer in the case of exceptions.</description>
		<content:encoded><![CDATA[<p>No, they (exceptions and overloading) aren&#8217;t comparable. And yet they are comparable.</p>
<p>They <i>aren&#8217;t</i> comparable because overloading results in a runtime reference to a method with a <i>specific</i> signature. Exceptions, on the other hand, are not specific&#8212;a single &#8220;catch&#8221; clause can catch many subtypes of exceptions.</p>
<p>They <i>are</i> comparable because with both exceptions and overloading, the Java specifications leave it to the programmer to deal with the potential mismatch between compile-time and run-time. The proposal would take that control out of the hands of the programmer in the case of exceptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rajiv</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-60</link>
		<dc:creator>rajiv</dc:creator>
		<pubDate>Sun, 01 Jul 2007 09:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-60</guid>
		<description>Doug:&lt;br/&gt;&lt;br/&gt;&lt;i&gt;The compiler cannot determine (with 100% accuracy) which exceptions will be specializations of which other exceptions when the code is actually executed.&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;The same limitation applies when compiler is trying to resolve which overloaded method is being invoked. The most specific overloaded method is chosen based on the inheritance hierarchy at compile time. Yes, this hierarchy could be broken at runtime during the invocation of the overloaded method, just like for exceptions. &lt;br/&gt;&lt;br/&gt;If the compromise is ok for overloaded methods it should be ok for catch clauses.</description>
		<content:encoded><![CDATA[<p>Doug:</p>
<p><i>The compiler cannot determine (with 100% accuracy) which exceptions will be specializations of which other exceptions when the code is actually executed.</i></p>
<p>The same limitation applies when compiler is trying to resolve which overloaded method is being invoked. The most specific overloaded method is chosen based on the inheritance hierarchy at compile time. Yes, this hierarchy could be broken at runtime during the invocation of the overloaded method, just like for exceptions. </p>
<p>If the compromise is ok for overloaded methods it should be ok for catch clauses.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Pardee</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-59</link>
		<dc:creator>Doug Pardee</dc:creator>
		<pubDate>Fri, 29 Jun 2007 17:17:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-59</guid>
		<description>I think that Sachin is partway there, but got derailed.&lt;br/&gt;&lt;br/&gt;Sometimes to understand the why&#039;s of Java, you need to remember that Java is 100% dynamically bound. The hierarchy of exceptions at runtime will not &lt;i&gt;necessarily&lt;/i&gt; be the same as the hierarchy that existed at compile time.&lt;br/&gt;&lt;br/&gt;The compiler cannot determine (with 100% accuracy) which exceptions will be specializations of which other exceptions when the code is actually executed. Reordering the clauses at compile time would work most of the time, but could result in erroneous results. This is something that is better left to the IDE, where the programmer can choose to do it or not and can intervene if the resulting order is inappropriate.</description>
		<content:encoded><![CDATA[<p>I think that Sachin is partway there, but got derailed.</p>
<p>Sometimes to understand the why&#8217;s of Java, you need to remember that Java is 100% dynamically bound. The hierarchy of exceptions at runtime will not <i>necessarily</i> be the same as the hierarchy that existed at compile time.</p>
<p>The compiler cannot determine (with 100% accuracy) which exceptions will be specializations of which other exceptions when the code is actually executed. Reordering the clauses at compile time would work most of the time, but could result in erroneous results. This is something that is better left to the IDE, where the programmer can choose to do it or not and can intervene if the resulting order is inappropriate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rajiv</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-58</link>
		<dc:creator>rajiv</dc:creator>
		<pubDate>Mon, 18 Jun 2007 04:17:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-58</guid>
		<description>Hey Sachin!&lt;br/&gt;&lt;br/&gt;&lt;i&gt;Sachin: I believe overloaded method calls are resolved at compilation time itself&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;Yes. Thanks for correcting!&lt;br/&gt;&lt;br/&gt;&lt;i&gt;Sachin: the question is still valid because the compiler can still be responsible for re-ordering catch clauses&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;Yes, that was what I was hoping the compiler would do.</description>
		<content:encoded><![CDATA[<p>Hey Sachin!</p>
<p><i>Sachin: I believe overloaded method calls are resolved at compilation time itself</i></p>
<p>Yes. Thanks for correcting!</p>
<p><i>Sachin: the question is still valid because the compiler can still be responsible for re-ordering catch clauses</i></p>
<p>Yes, that was what I was hoping the compiler would do.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sachin</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-57</link>
		<dc:creator>Sachin</dc:creator>
		<pubDate>Sun, 17 Jun 2007 07:06:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-57</guid>
		<description>Excellent read!&lt;br/&gt;&lt;br/&gt;My thoughts on this topic - but before that I just wanted to mention that I believe overloaded method calls are resolved at compilation time itself and are statically determined so there is no way the &#039;right&#039; overloaded method call can be made cheaply at runtime. It is method overriding (polymorphic behaviour) that is dynamically determined.&lt;br/&gt;&lt;br/&gt;I think it is possible that runtime performance of exception handling is a big reason why the Java runtime expects exception handlers to be organized the way they are (most specialized to most generalized). I guess the exception handling code can then just work its way down the list and the first handler that returns true for Class.isAssignableFrom would work (I don&#039;t know what the actually byte code generated is so I may be wrong). If it was required to determine the best handler out of them all at runtime, all handlers have to be visited and the best one recovered at runtime making it much slower.&lt;br/&gt;&lt;br/&gt;However, despite this the question is still valid because the compiler can still be responsible for re-ordering catch clauses as expected by the runtime and not pose the restriction on the programmer. Here, I believe the reasoning could very well be usability. I would argue that providing this &#039;flexibility&#039; is less usable because, although there is more flexibility while programming, the code becomes less maintainable. It is no longer possible to read the exception handling code without manually performing this operation every time you read such code. Making the programmer do it once is an overhead that saves time many times over.&lt;br/&gt;&lt;br/&gt;If Java were designed with a philosophy of &quot;more power to the programmer&quot; then I would expect this would be supported (and we would then have had someone suggest as a good coding guideline that everyone organize their catch clauses this way for readability) however Java does take the more restrictive approach to programmer flexibility and this seems to be in keeping with that.</description>
		<content:encoded><![CDATA[<p>Excellent read!</p>
<p>My thoughts on this topic &#8211; but before that I just wanted to mention that I believe overloaded method calls are resolved at compilation time itself and are statically determined so there is no way the &#8216;right&#8217; overloaded method call can be made cheaply at runtime. It is method overriding (polymorphic behaviour) that is dynamically determined.</p>
<p>I think it is possible that runtime performance of exception handling is a big reason why the Java runtime expects exception handlers to be organized the way they are (most specialized to most generalized). I guess the exception handling code can then just work its way down the list and the first handler that returns true for Class.isAssignableFrom would work (I don&#8217;t know what the actually byte code generated is so I may be wrong). If it was required to determine the best handler out of them all at runtime, all handlers have to be visited and the best one recovered at runtime making it much slower.</p>
<p>However, despite this the question is still valid because the compiler can still be responsible for re-ordering catch clauses as expected by the runtime and not pose the restriction on the programmer. Here, I believe the reasoning could very well be usability. I would argue that providing this &#8216;flexibility&#8217; is less usable because, although there is more flexibility while programming, the code becomes less maintainable. It is no longer possible to read the exception handling code without manually performing this operation every time you read such code. Making the programmer do it once is an overhead that saves time many times over.</p>
<p>If Java were designed with a philosophy of &#8220;more power to the programmer&#8221; then I would expect this would be supported (and we would then have had someone suggest as a good coding guideline that everyone organize their catch clauses this way for readability) however Java does take the more restrictive approach to programmer flexibility and this seems to be in keeping with that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-56</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Fri, 15 Jun 2007 20:55:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-56</guid>
		<description>I think the answer may be simpler than that: like most of the worst parts of the original Java spec (let&#039;s all cheer for breaking after each switch case), this behavior is exactly the same as C++.</description>
		<content:encoded><![CDATA[<p>I think the answer may be simpler than that: like most of the worst parts of the original Java spec (let&#8217;s all cheer for breaking after each switch case), this behavior is exactly the same as C++.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rupesh Kumar</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-55</link>
		<dc:creator>Rupesh Kumar</dc:creator>
		<pubDate>Thu, 14 Jun 2007 18:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-55</guid>
		<description>btw dude, &quot;Secret of The Masters&quot; was awesome ;-) LOL !! Looks like JDJ is not getting any articles to publish..</description>
		<content:encoded><![CDATA[<p>btw dude, &#8220;Secret of The Masters&#8221; was awesome <img src='http://www.0xcafefeed.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  LOL !! Looks like JDJ is not getting any articles to publish..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rupesh Kumar</title>
		<link>http://www.0xcafefeed.com/2007/06/why-do-catch-clauses-need-to-be-ordered/comment-page-1/#comment-54</link>
		<dc:creator>Rupesh Kumar</dc:creator>
		<pubDate>Thu, 14 Jun 2007 18:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.0xCAFEFEED.com/?p=16#comment-54</guid>
		<description>Wow.. Thats really interesting. I had never thought about this. We are sooo used to follow the specification rather than question it :-)&lt;br/&gt;This made me look into our CFML compiler and the way it invokes the catch clause. Since CFML exceptions are ultilately java exceptions and the compiler is also written in java, we catch Throwable and do the exact match for all catch types. So there is no order needed for CFML.&lt;br/&gt;I really wonder why Java was not done that way.&lt;br/&gt;&lt;br/&gt;My guess is that perhaps these guys first decided to have mutiple inheritance in java and write exception handling based on that. They later backed multiple inheritance off but left this as it was :-D</description>
		<content:encoded><![CDATA[<p>Wow.. Thats really interesting. I had never thought about this. We are sooo used to follow the specification rather than question it <img src='http://www.0xcafefeed.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> <br />This made me look into our CFML compiler and the way it invokes the catch clause. Since CFML exceptions are ultilately java exceptions and the compiler is also written in java, we catch Throwable and do the exact match for all catch types. So there is no order needed for CFML.<br />I really wonder why Java was not done that way.</p>
<p>My guess is that perhaps these guys first decided to have mutiple inheritance in java and write exception handling based on that. They later backed multiple inheritance off but left this as it was <img src='http://www.0xcafefeed.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
