<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sadek Drobi's Blog &#187; Spring</title>
	<atom:link href="http://sadekdrobi.com/category/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://sadekdrobi.com</link>
	<description>Sadek Drobi</description>
	<lastBuildDate>Tue, 08 Mar 2011 22:56:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Who told Java Checked Exceptions were a Bad Idea?</title>
		<link>http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/</link>
		<comments>http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 00:50:22 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[Decipline]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/</guid>
		<description><![CDATA[
One of the few things that lack seriously, IMO, in C# is the power of Java checked exceptions. In Java world? things happen weirdly. Maybe it is a result of crowd intelligence and a bad side effect of open source. Often, people in J2EE world tend to believe the opposite. Anyway you see the effect [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/01/dsc-02851.jpg"><img style="border-right: 0px; border-top: 0px; margin: 0px 15px 10px 0px; border-left: 0px; border-bottom: 0px" height="315" alt="DSC_0285" src="http://sadekdrobi.com/wp-content/uploads/2009/01/dsc-0285-thumb1.jpg" width="459" align="left" border="0"></a>
<p>One of the few things that lack seriously, IMO, in C# is the power of Java checked exceptions. In Java world? things happen weirdly. Maybe it is a result of <a href="http://en.wikipedia.org/wiki/The_Wisdom_of_Crowds#Failures_of_crowd_intelligence">crowd intelligence</a> and a bad side effect of open source. Often, people in J2EE world tend to believe the opposite. Anyway you see the effect in most preferred frameworks (such as today&#8217;s popular ORMs and Containers) of wrapping checked Java exceptions and rethrowing them as runtime ones. </p>
<p><span id="more-569"></span></p>
<p>I guess offering runtime exceptions together with compile time ones did a hole in the Java type system, but this hole was left to discipline, in a way of giving the tools and letting smart enterprise programmers decide for the right tool for the job. Things turned to happen differently in the enterprise world.&nbsp; Programmers abused of this freedom of choice and mainstream frameworks designers decided to dismiss and bury one of the most interesting features of the Java type system.</p>
<p>Why do I like Java checked exceptions? Well simply because it saves me from surprises. And I tend not to like surprises much!</p>
<p>The funny thing is that, in my experience, it is mostly architects that dismiss Java checked exceptions whereas Compile time exceptions are a very good asset for cleaner and bug free architecture.</p>
<p>I just take a look at a &#8220;standard&#8221; DAO method that gets an entity</p>
<p>Customer getCustomer(CustomerId id);</p>
<p>This method lies! what if there is no connection? what if the Id is incorrect? The signature of the methods gives quite a promise that under all circumstances it will returns a Customer. Now for any architecture this is bad. It used to be better before some popular frameworks decided to completely omit it! Before? well I used to control my architecture better!</p>
<p>Before these &#8220;good practices&#8221; become mainstream, imposed in mainstream frameworks, It used to be often more predictable the return type a method has. For my example, the signature would tell me that the method will either return a customer OR throws a &#8220;Dao Exception&#8221;.</p>
<p>Customer getCustomer(CustomerId id) throws DaoException;</p>
<p>No surprise. Now when I use this method, I will certainly need to handle the case when the method will return something else than a Customer. Now of course, a Java &#8220;enterprise developer&#8221; could do one of to things:</p>
<p>1- Out of laziness or incompetence, keep rethrowing data access exceptions up until you&#8217;ll have to deal with it up in the GUI logic (where handling an infrastructure error does not often belong)</p>
<p>This often sounds for me the main argument for dismissing Java Compile Time Exceptions claiming that it is a &#8220;code boilerplate&#8221;. The thing is that you&#8217;ll have to deal with these exceptions once even when you turn them into runtime. The fact of wrapping them into runtime does not eliminate them (even if I wish it did). But having them as compile time will make easy to spot where a &#8220;bad&#8221; developer did not handle the exception correctly simply by following the signature and type system. Then the solution will be to wrap them into more business friendly, context meaningful Compile Time Exceptions that need to be handled. This way the contract communicated better the result of executing a piece of logic.</p>
<p>This turns to be as useful for &#8220;void&#8221; return type methods. With Compile Time Exception a method can simply communicate the fact that it will either return &#8220;void&#8221; or will throw an exception to be handled.</p>
<p>2- Out of laziness or incompetence, frustrated developers will start &#8220;catch&#8221;ing checked exception without rethrowing them.</p>
<p>This one is a result of the hole in the type system that is left intentionally to leave some place for flexibility relying on discipline. Nevertheless, and before the big favor of generous frameworks I mentioned earlier, this turned out to be an easy problem to spot. Most IDEs and code analyses tools can find easily empty catch clauses and warn me about them.</p>
<p>&nbsp;</p>
<p>I often get surprised of the limited use of Java language wealth in enterprise J2EE applications!</p>
<p>Good thing: C# and .Net are introducing checked exceptions together with other DbC stuff (like non null objects) in the next release. Sad thing: Java has Compile Time Exceptions since version 1, open source chose to unvalue them!</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

