<?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: How to (not) handle Java Exceptions</title>
	<atom:link href="http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/</link>
	<description>Roman Kennke's ramblings</description>
	<pubDate>Fri, 21 Nov 2008 10:47:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Andrew John Hughes</title>
		<link>http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/#comment-64659</link>
		<dc:creator>Andrew John Hughes</dc:creator>
		<pubDate>Tue, 20 May 2008 11:28:59 +0000</pubDate>
		<guid isPermaLink="false">http://kennke.org/blog/?p=179#comment-64659</guid>
		<description>Throughly excellent blog and couldn't agree more.  I not only see students doing this, but many academics actually teaching with code that does this.  It's pure evil!

The usual shorthand I use when there is no appropriate constructor is:

throw new InternalError(msg).initCause()

as initCause returns the original throwable.  You may need to cast as well to get the right exception for the throws statement.  Someone could really do with adding the appropriate constructors to all the API ones though; does this really need a JSR?

The message also annoys me as so many times people don't include state information that would much cut down the debugging time e.g. "Invalid format" or "Incorrect pattern" -- why not include the format or pattern in the string as well?!?</description>
		<content:encoded><![CDATA[<p>Throughly excellent blog and couldn&#8217;t agree more.  I not only see students doing this, but many academics actually teaching with code that does this.  It&#8217;s pure evil!</p>
<p>The usual shorthand I use when there is no appropriate constructor is:</p>
<p>throw new InternalError(msg).initCause()</p>
<p>as initCause returns the original throwable.  You may need to cast as well to get the right exception for the throws statement.  Someone could really do with adding the appropriate constructors to all the API ones though; does this really need a JSR?</p>
<p>The message also annoys me as so many times people don&#8217;t include state information that would much cut down the debugging time e.g. &#8220;Invalid format&#8221; or &#8220;Incorrect pattern&#8221; &#8212; why not include the format or pattern in the string as well?!?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jilles van Gurp</title>
		<link>http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/#comment-64644</link>
		<dc:creator>Jilles van Gurp</dc:creator>
		<pubDate>Sat, 17 May 2008 04:04:56 +0000</pubDate>
		<guid isPermaLink="false">http://kennke.org/blog/?p=179#comment-64644</guid>
		<description>You are absolutely right on this. I've wasted countless hours on broken assumptions such as "this exception never happens". 

On the other hand, the standard Java library is full of inappropriate use of Exceptions that you might strongly argue actually don't happen or only happen if you fail to do some elementary checking on your data. A run time exception is much more appropriate in such cases. 

For example, I've encountered many times that I have to catch a UnsupportedEncodingException exception while I am pretty sure that a lack of support for utf-8 is pretty rare. So my code reads setEncoding("utf-8"); and I am forced to deal with the possibility that utf-8 is somehow not supported.

I always log this type of exception at severe level because if it does happen something is seriously wrong with my code (e.g. typo in the utf-8). But it's tedious to have to write this code.</description>
		<content:encoded><![CDATA[<p>You are absolutely right on this. I&#8217;ve wasted countless hours on broken assumptions such as &#8220;this exception never happens&#8221;. </p>
<p>On the other hand, the standard Java library is full of inappropriate use of Exceptions that you might strongly argue actually don&#8217;t happen or only happen if you fail to do some elementary checking on your data. A run time exception is much more appropriate in such cases. </p>
<p>For example, I&#8217;ve encountered many times that I have to catch a UnsupportedEncodingException exception while I am pretty sure that a lack of support for utf-8 is pretty rare. So my code reads setEncoding(&#8221;utf-8&#8243;); and I am forced to deal with the possibility that utf-8 is somehow not supported.</p>
<p>I always log this type of exception at severe level because if it does happen something is seriously wrong with my code (e.g. typo in the utf-8). But it&#8217;s tedious to have to write this code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ismael Juma</title>
		<link>http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/#comment-64639</link>
		<dc:creator>Ismael Juma</dc:creator>
		<pubDate>Sat, 17 May 2008 00:03:00 +0000</pubDate>
		<guid isPermaLink="false">http://kennke.org/blog/?p=179#comment-64639</guid>
		<description>Oh well, seems like I was too slow. :)

Ismael</description>
		<content:encoded><![CDATA[<p>Oh well, seems like I was too slow. <img src='http://kennke.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Ismael</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ismael Juma</title>
		<link>http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/#comment-64638</link>
		<dc:creator>Ismael Juma</dc:creator>
		<pubDate>Sat, 17 May 2008 00:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://kennke.org/blog/?p=179#comment-64638</guid>
		<description>Hi,

You're obviously right that swallowing exceptions is very bad practice, but in most cases you can rethrow an exception with just one line instead of three by passing the cause in the constructor (which eliminates the need for the temporary variable). Of course this only works with exceptions that take a Throwable in the constructor, but this is quite common from my experience.

Regards,
Ismael</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>You&#8217;re obviously right that swallowing exceptions is very bad practice, but in most cases you can rethrow an exception with just one line instead of three by passing the cause in the constructor (which eliminates the need for the temporary variable). Of course this only works with exceptions that take a Throwable in the constructor, but this is quite common from my experience.</p>
<p>Regards,<br />
Ismael</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uzytkownik</title>
		<link>http://kennke.org/blog/2008/05/16/how-to-not-handle-java-exceptions/#comment-64637</link>
		<dc:creator>Uzytkownik</dc:creator>
		<pubDate>Fri, 16 May 2008 23:59:58 +0000</pubDate>
		<guid isPermaLink="false">http://kennke.org/blog/?p=179#comment-64637</guid>
		<description>I'm not sure but I think in most exceptions (standard) you can do:
catch (SomeException ex) {
  throw new SomeOtherException(ex);
}
Which will print both stacks.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure but I think in most exceptions (standard) you can do:<br />
catch (SomeException ex) {<br />
  throw new SomeOtherException(ex);<br />
}<br />
Which will print both stacks.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
