<?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"
	>
<channel>
	<title>Comments on: Internal and External Exceptions</title>
	<atom:link href="http://cafe.elharo.com/blogroll/internal-and-external-exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/</link>
	<description>Longer than a blog; shorter than a book</description>
	<pubDate>Sun, 07 Sep 2008 18:10:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Hwo to handle java Exceptions? &#171; Stefon&#8217;s Blog</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-247171</link>
		<dc:creator>Hwo to handle java Exceptions? &#171; Stefon&#8217;s Blog</dc:creator>
		<pubDate>Fri, 11 Jul 2008 13:19:22 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-247171</guid>
		<description>[...] Source: The Cafes [...]</description>
		<content:encoded><![CDATA[<p>[...] Source: The Cafes [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kjetil Valstadsve</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-114534</link>
		<dc:creator>Kjetil Valstadsve</dc:creator>
		<pubDate>Sun, 29 Jul 2007 09:37:43 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-114534</guid>
		<description>Roland, 

It is tedious to HAVE TO do it. 

It CAN BE useful to wrap and contribute some contextual, runtime information to the stacktrace. 

It IS unnecessary because you can use unchecked exceptions instead.</description>
		<content:encoded><![CDATA[<p>Roland, </p>
<p>It is tedious to HAVE TO do it. </p>
<p>It CAN BE useful to wrap and contribute some contextual, runtime information to the stacktrace. </p>
<p>It IS unnecessary because you can use unchecked exceptions instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Pibinger</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-114324</link>
		<dc:creator>Roland Pibinger</dc:creator>
		<pubDate>Sat, 28 Jul 2007 20:36:38 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-114324</guid>
		<description>@Thomas Pollinger
It would be nice if Java automatically chained Exceptions according to the Exception specified by the method. Manual wrapping as in: 

&lt;pre&gt;catch (SQLException ex) { 
  throw new DomainModelException(ex); 
} &lt;/pre&gt;

is tedious and unnecessary.</description>
		<content:encoded><![CDATA[<p>@Thomas Pollinger<br />
It would be nice if Java automatically chained Exceptions according to the Exception specified by the method. Manual wrapping as in: </p>
<pre>catch (SQLException ex) {
  throw new DomainModelException(ex);
} </pre>
<p>is tedious and unnecessary.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kjetil Valstadsve</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-114162</link>
		<dc:creator>Kjetil Valstadsve</dc:creator>
		<pubDate>Sat, 28 Jul 2007 09:46:29 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-114162</guid>
		<description>Thomas, I was pointing to the exact same thing. Not wrapping the cause generally forces you to open the debugger and re-run, hoping to find out what it is - and that is assuming it is reproducible and relatively easy to pick out. (Think of code that runs on thousands of elements - one of which provokes the error!) In lucky cases, someone has logged the cause somewhere just before (also an anti-pattern), or you have the code and can find a likely culprit. But not knowing what failed is NOT encapsulation, it's more like subterfuge.

For further payoffs, provide an additional message in the new exception, containing some contextual information relevant to the situation. Keep in mind that more information exists at runtime, and all too often I see static exception message strings thrown from, say, lookup-foo-by-name type methods, simply: "Unknown foo". What a senseless waste of human life! How many man-hours could not have been saved, if the author instead had given us the string "Unknown foo  " + name?

The runtime perspective is a good thing, and helps me produce transparent code that can tell me about errors and help me find its right shape. I fear the static perspective - including trying to predict errors with exception checking - is confusing and may suggest, e.g. to people learning Java, that this practice is somehow "illegal" design-wise. I have been met with the argument, particularly from recovering C++ victims - that wrapping the lower-level exception will "reveal" the implementation.</description>
		<content:encoded><![CDATA[<p>Thomas, I was pointing to the exact same thing. Not wrapping the cause generally forces you to open the debugger and re-run, hoping to find out what it is - and that is assuming it is reproducible and relatively easy to pick out. (Think of code that runs on thousands of elements - one of which provokes the error!) In lucky cases, someone has logged the cause somewhere just before (also an anti-pattern), or you have the code and can find a likely culprit. But not knowing what failed is NOT encapsulation, it&#8217;s more like subterfuge.</p>
<p>For further payoffs, provide an additional message in the new exception, containing some contextual information relevant to the situation. Keep in mind that more information exists at runtime, and all too often I see static exception message strings thrown from, say, lookup-foo-by-name type methods, simply: &#8220;Unknown foo&#8221;. What a senseless waste of human life! How many man-hours could not have been saved, if the author instead had given us the string &#8220;Unknown foo  &#8221; + name?</p>
<p>The runtime perspective is a good thing, and helps me produce transparent code that can tell me about errors and help me find its right shape. I fear the static perspective - including trying to predict errors with exception checking - is confusing and may suggest, e.g. to people learning Java, that this practice is somehow &#8220;illegal&#8221; design-wise. I have been met with the argument, particularly from recovering C++ victims - that wrapping the lower-level exception will &#8220;reveal&#8221; the implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas Pollinger</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-113683</link>
		<dc:creator>Thomas Pollinger</dc:creator>
		<pubDate>Fri, 27 Jul 2007 00:50:22 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-113683</guid>
		<description>This was an interesting read and I mostly agree with the outlined suggestions. Particularly, I often encapsulate checked exceptions as checked domain exceptions so that I do not expose implementation detail exception to the outer layers that don't need to.

One thing that should &lt;b&gt;NEVER&lt;/b&gt; be done in production code (quite frankly, not even in test code or whatever code - I see that sadly all too often and has caused a lot of grieve) is given by the example in Elliotte's code snippet that illustrates how to encapsulate domain specific exceptions:


try {
&#160;&#160;someDatabaseAccessingMethod();
} catch (SQLExcepiton ex) {
&#160;&#160;throw new DomainModelException(&lt;b&gt;ex.getMessage()&lt;/b&gt;);
}


&lt;code&gt;ex&lt;/code&gt; &lt;b&gt;has&lt;/b&gt; to be passed along as an exception cause inside &lt;code&gt;DomainModelException&lt;/code&gt;. 

The code snippet needs to read as follows:


try {
&#160;&#160;someDatabaseAccessingMethod();
} catch (SQLExcepiton ex) {
&#160;&#160;throw new DomainModelException(&lt;b&gt;ex&lt;/b&gt;);
}


I would put the former code snippet as an example of antipattern number 1 in the list of Java antipatterns.</description>
		<content:encoded><![CDATA[<p>This was an interesting read and I mostly agree with the outlined suggestions. Particularly, I often encapsulate checked exceptions as checked domain exceptions so that I do not expose implementation detail exception to the outer layers that don&#8217;t need to.</p>
<p>One thing that should <b>NEVER</b> be done in production code (quite frankly, not even in test code or whatever code - I see that sadly all too often and has caused a lot of grieve) is given by the example in Elliotte&#8217;s code snippet that illustrates how to encapsulate domain specific exceptions:</p>
<p>try {<br />
&nbsp;&nbsp;someDatabaseAccessingMethod();<br />
} catch (SQLExcepiton ex) {<br />
&nbsp;&nbsp;throw new DomainModelException(<b>ex.getMessage()</b>);<br />
}</p>
<p><code>ex</code> <b>has</b> to be passed along as an exception cause inside <code>DomainModelException</code>. </p>
<p>The code snippet needs to read as follows:</p>
<p>try {<br />
&nbsp;&nbsp;someDatabaseAccessingMethod();<br />
} catch (SQLExcepiton ex) {<br />
&nbsp;&nbsp;throw new DomainModelException(<b>ex</b>);<br />
}</p>
<p>I would put the former code snippet as an example of antipattern number 1 in the list of Java antipatterns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Bayko</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-112969</link>
		<dc:creator>John Bayko</dc:creator>
		<pubDate>Tue, 24 Jul 2007 20:50:17 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-112969</guid>
		<description>I wrote a bit about error handling here, which covered checked and unchecked exceptions:

http://www.artima.com/forums/flat.jsp?forum=270&#38;thread=207358#269379

In brief, a checked exception is part of the status information (success/fail, diagnostics) passed from a routine to the calling code, and so it's legitimate to have to declare it. If not an exception, it would be an extra status parameter or return value, which would also need to be declared. Think of an exception as an "invisible return parameter".

Unchecked exceptions a very much like gotos - untraceable execution/data paths. I find it odd that they have so many defenders.</description>
		<content:encoded><![CDATA[<p>I wrote a bit about error handling here, which covered checked and unchecked exceptions:</p>
<p><a href="http://www.artima.com/forums/flat.jsp?forum=270&amp;thread=207358#269379" rel="nofollow">http://www.artima.com/forums/flat.jsp?forum=270&amp;thread=207358#269379</a></p>
<p>In brief, a checked exception is part of the status information (success/fail, diagnostics) passed from a routine to the calling code, and so it&#8217;s legitimate to have to declare it. If not an exception, it would be an extra status parameter or return value, which would also need to be declared. Think of an exception as an &#8220;invisible return parameter&#8221;.</p>
<p>Unchecked exceptions a very much like gotos - untraceable execution/data paths. I find it odd that they have so many defenders.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kjetil Valstadsve</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-112901</link>
		<dc:creator>Kjetil Valstadsve</dc:creator>
		<pubDate>Tue, 24 Jul 2007 15:20:04 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-112901</guid>
		<description>Elliotte, I know that is the other option. I've been through this. I also know that you should and must wrap the original exception, or you will lose valuable failure information. If you don't, you will just send the reader of your stacktrace (usually yourself) out on a time-consuming debug session. You probably know this too, so I'm assuming your example was a bit rushed.

But it does bring me onto the next objection I have about checked exceptions. The following is a re-hash of my postings in your previous thread on exceptions, but the gist of it is, that you will eventually end up with ALL methods declaring that they throws DomainModelException. This is nice from a design point of view, in that each layer will have its own exception (super)class, dutifully declared in every method. 

However, I find that this practice doesn't provide THAT much more information than NOT having that throws in every method. What it does provide though, is the need for duplicating that particular try/catch/throw construct in most (if not all) method calls going down-layer, in all layers. This is a high cost, and the information gain is low.

And when you look at what comes out, in the end, when something fails and assuming one follows your practice of using e.getMessage() for the message - what information does it contain? It has exactly the same information as an unchecked exception thrown at the original cause would have had, it just has a lot more noise. And yes, it should be handled, no rugsweeping here, no sir - but just because it's important doesn't mean it warrants superfluous effort.

I am trying to put this simply. I know I should give up and get on with the coding :-)</description>
		<content:encoded><![CDATA[<p>Elliotte, I know that is the other option. I&#8217;ve been through this. I also know that you should and must wrap the original exception, or you will lose valuable failure information. If you don&#8217;t, you will just send the reader of your stacktrace (usually yourself) out on a time-consuming debug session. You probably know this too, so I&#8217;m assuming your example was a bit rushed.</p>
<p>But it does bring me onto the next objection I have about checked exceptions. The following is a re-hash of my postings in your previous thread on exceptions, but the gist of it is, that you will eventually end up with ALL methods declaring that they throws DomainModelException. This is nice from a design point of view, in that each layer will have its own exception (super)class, dutifully declared in every method. </p>
<p>However, I find that this practice doesn&#8217;t provide THAT much more information than NOT having that throws in every method. What it does provide though, is the need for duplicating that particular try/catch/throw construct in most (if not all) method calls going down-layer, in all layers. This is a high cost, and the information gain is low.</p>
<p>And when you look at what comes out, in the end, when something fails and assuming one follows your practice of using e.getMessage() for the message - what information does it contain? It has exactly the same information as an unchecked exception thrown at the original cause would have had, it just has a lot more noise. And yes, it should be handled, no rugsweeping here, no sir - but just because it&#8217;s important doesn&#8217;t mean it warrants superfluous effort.</p>
<p>I am trying to put this simply. I know I should give up and get on with the coding <img src='http://cafe.elharo.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-112868</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Tue, 24 Jul 2007 12:21:16 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-112868</guid>
		<description>Elliotte,
I did not say the throwing method has to be 100% sure that the calling method will be able to handle it, but there does need to be a reasonable expectation that it will be able to do something useful.  The reason I took the more extreme view that it should be the &lt;i&gt;calling&lt;/i&gt; method that should be expected to handle it as opposed to someone else up the chain (and note that I did not say Bloch said that, though his wording was "the programmer using the API" which is fairly close) is that if the exception is unchecked, the calling method gets to decide how to handle it.  They are free to catch it or document that it is thrown if they like, or ignore it as something that it cannot do a damn thing about and leave it for some top level method.  You are right, the throwing method doesn't usually know what the calling method can or cannot do, so in those cases leave the decision to the calling method.  

And of course throwing an unchecked exception doesn't mean don't document it anywhere.  If your method might throw an unchecked exception, you need to let the calling method know through the javadoc so it will be able to make that decision.

Forcing methods to deal with exceptions they cannot handle will result in one of three things: empty or nearly empty catch blocks as described by Igno (and in my experience, that happens a lot), throws clauses that grow exponentially as more and more methods get called like I described in my first post, or wrapped exceptions that just results in the obfuscation of the original, useful exception.  When something happens like a resource is missing, often the only thing they can do is have a top level method (the container or shell or GUI or whatever) log the exception and print out an error message.  

Also, it may be nitpicking, but I disagree regarding when Errors should be thrown instead of Exceptions.  If you can handle it 10% of the time it should probably be an unchecked exception, as Errors are (almost) never handled.  Something like an OutOfMemoryError cannot be handled even 10% of the time; if the system ran out of memory, then your app is pretty much hosed.  The only Error I can think of offhand that can be properly handled is a StackOverflowError since they are often thrown by recursion gone bad.</description>
		<content:encoded><![CDATA[<p>Elliotte,<br />
I did not say the throwing method has to be 100% sure that the calling method will be able to handle it, but there does need to be a reasonable expectation that it will be able to do something useful.  The reason I took the more extreme view that it should be the <i>calling</i> method that should be expected to handle it as opposed to someone else up the chain (and note that I did not say Bloch said that, though his wording was &#8220;the programmer using the API&#8221; which is fairly close) is that if the exception is unchecked, the calling method gets to decide how to handle it.  They are free to catch it or document that it is thrown if they like, or ignore it as something that it cannot do a damn thing about and leave it for some top level method.  You are right, the throwing method doesn&#8217;t usually know what the calling method can or cannot do, so in those cases leave the decision to the calling method.  </p>
<p>And of course throwing an unchecked exception doesn&#8217;t mean don&#8217;t document it anywhere.  If your method might throw an unchecked exception, you need to let the calling method know through the javadoc so it will be able to make that decision.</p>
<p>Forcing methods to deal with exceptions they cannot handle will result in one of three things: empty or nearly empty catch blocks as described by Igno (and in my experience, that happens a lot), throws clauses that grow exponentially as more and more methods get called like I described in my first post, or wrapped exceptions that just results in the obfuscation of the original, useful exception.  When something happens like a resource is missing, often the only thing they can do is have a top level method (the container or shell or GUI or whatever) log the exception and print out an error message.  </p>
<p>Also, it may be nitpicking, but I disagree regarding when Errors should be thrown instead of Exceptions.  If you can handle it 10% of the time it should probably be an unchecked exception, as Errors are (almost) never handled.  Something like an OutOfMemoryError cannot be handled even 10% of the time; if the system ran out of memory, then your app is pretty much hosed.  The only Error I can think of offhand that can be properly handled is a StackOverflowError since they are often thrown by recursion gone bad.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elliotte Rusty Harold</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-112849</link>
		<dc:creator>Elliotte Rusty Harold</dc:creator>
		<pubDate>Tue, 24 Jul 2007 11:04:19 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-112849</guid>
		<description>I do not agree that throwing checked exceptions that the &lt;em&gt;calling&lt;/em&gt; method cannot be expected to handle is an antipattern. The throwing method in general knows nothing about what the calling method can or cannot handle. Some methods that call it may be able to handle the exception. Others may not, and have to rethrow it. Regardless, the throwing method never expects that the calling method can handle the exception and the throwing method never expects that the calling method can't handle the exception. Its job is to notify the calling method of the exception, not to worry about how the calling method will or will not handle it. 

I'll have to reread Bloch, but I think what he meant was not whether the &lt;em&gt;calling&lt;/em&gt; method could be expected to handle it, but whether any method in the call chain could (and if he didn't mean that, he should have). There are cases such as &lt;code&gt;OutOfMemoryError&lt;/code&gt; that really can't be handled properly 90% of the time. These are represented by errors, not exceptions (either checked or runtime) and they're very uncommon.</description>
		<content:encoded><![CDATA[<p>I do not agree that throwing checked exceptions that the <em>calling</em> method cannot be expected to handle is an antipattern. The throwing method in general knows nothing about what the calling method can or cannot handle. Some methods that call it may be able to handle the exception. Others may not, and have to rethrow it. Regardless, the throwing method never expects that the calling method can handle the exception and the throwing method never expects that the calling method can&#8217;t handle the exception. Its job is to notify the calling method of the exception, not to worry about how the calling method will or will not handle it. </p>
<p>I&#8217;ll have to reread Bloch, but I think what he meant was not whether the <em>calling</em> method could be expected to handle it, but whether any method in the call chain could (and if he didn&#8217;t mean that, he should have). There are cases such as <code>OutOfMemoryError</code> that really can&#8217;t be handled properly 90% of the time. These are represented by errors, not exceptions (either checked or runtime) and they&#8217;re very uncommon.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ingo</title>
		<link>http://cafe.elharo.com/blogroll/internal-and-external-exceptions/#comment-112826</link>
		<dc:creator>Ingo</dc:creator>
		<pubDate>Tue, 24 Jul 2007 07:15:26 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/internal-and-external-exceptions/#comment-112826</guid>
		<description>Elliot, on the academic side you are most probably right in respect to checked vs unchecked (I'm not so sure regarding internal vs external). When checked exceptions where introduced into Java the idea was to force people to use and to handle these to make sure people did not ignore them. Reality however showed that people who do not care about good exception handling will find ways around it - shortcuts that made it worse, not better. If you look into real code you'll find empty catches and areas catching or throwing just exception.
Throwing unchecked exceptions doesn't make it worse nor better. You are still free to cheat and avoid dealing with them and you are still free to handle and deal with them.
You do however make it easier to deal with them collectively in one area and avoid having to deal (if only to re-throw or wrap) with them in multiple areas - each one an area where someone can deal with them wrongly.
At the end it comes back to the question of what's more important - academic correctness or pragmatism.
Personally I believe the later to be more important, which is the main theme of my blog (&lt;a href="http://goldentoolbox.blogspot.com/" rel="nofollow"&gt;Golden Toolbox&lt;/a&gt;. Sorry for the shameless plug - however there are a couple of posts where you now have the chance to critique me :-).</description>
		<content:encoded><![CDATA[<p>Elliot, on the academic side you are most probably right in respect to checked vs unchecked (I&#8217;m not so sure regarding internal vs external). When checked exceptions where introduced into Java the idea was to force people to use and to handle these to make sure people did not ignore them. Reality however showed that people who do not care about good exception handling will find ways around it - shortcuts that made it worse, not better. If you look into real code you&#8217;ll find empty catches and areas catching or throwing just exception.<br />
Throwing unchecked exceptions doesn&#8217;t make it worse nor better. You are still free to cheat and avoid dealing with them and you are still free to handle and deal with them.<br />
You do however make it easier to deal with them collectively in one area and avoid having to deal (if only to re-throw or wrap) with them in multiple areas - each one an area where someone can deal with them wrongly.<br />
At the end it comes back to the question of what&#8217;s more important - academic correctness or pragmatism.<br />
Personally I believe the later to be more important, which is the main theme of my blog (<a href="http://goldentoolbox.blogspot.com/" rel="nofollow">Golden Toolbox</a>. Sorry for the shameless plug - however there are a couple of posts where you now have the chance to critique me :-).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
