<?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: Notes on exception handling	</title>
	<atom:link href="http://matteo.vaccari.name/blog/archives/875/feed" rel="self" type="application/rss+xml" />
	<link>http://matteo.vaccari.name/blog/archives/875</link>
	<description>Extreme enthusiasm</description>
	<lastBuildDate>
	Mon, 25 Feb 2019 15:18:16 +0000	</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.1.1</generator>
			<item>
				<title>
				By: Philip Schwarz				</title>
				<link>http://matteo.vaccari.name/blog/archives/875/comment-page-1#comment-96411</link>
		<dc:creator><![CDATA[Philip Schwarz]]></dc:creator>
		<pubDate>Sat, 30 Nov 2013 20:48:35 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=875#comment-96411</guid>
					<description><![CDATA[One way in which Nat Pryce, author of Growing Object Oriented Software - Guided by Tests, stressed  the kind of error that RuntimeExceptions are designed to report, was by blogging (http://nat.truemesh.com/archives/000643.html) that RuntimeException be renamed to StupidProgrammerException. Another way he did that was by suggesting (http://www.natpryce.com/articles/000739.html) that checked exceptions due to programming errors be converted in a dedicated Defect exception extending RuntimeException.]]></description>
		<content:encoded><![CDATA[<p>One way in which Nat Pryce, author of Growing Object Oriented Software &#8211; Guided by Tests, stressed  the kind of error that RuntimeExceptions are designed to report, was by blogging (<a href="http://nat.truemesh.com/archives/000643.html" rel="nofollow">http://nat.truemesh.com/archives/000643.html</a>) that RuntimeException be renamed to StupidProgrammerException. Another way he did that was by suggesting (<a href="http://www.natpryce.com/articles/000739.html" rel="nofollow">http://www.natpryce.com/articles/000739.html</a>) that checked exceptions due to programming errors be converted in a dedicated Defect exception extending RuntimeException.</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: Philip Schwarz				</title>
				<link>http://matteo.vaccari.name/blog/archives/875/comment-page-1#comment-96409</link>
		<dc:creator><![CDATA[Philip Schwarz]]></dc:creator>
		<pubDate>Sat, 30 Nov 2013 19:38:07 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=875#comment-96409</guid>
					<description><![CDATA[Willie Wheeler, author of &#039;Spring in Practice&#039; has a name for the second concern I have with catching Exception: &#039;Mask via broad catch, and misreport&#039; (see Wheeler&#039;s answer to StackOverflow question: http://stackoverflow.com/questions/6591470/checked-vs-unchecked-exceptions-in-service-layer).

Philip]]></description>
		<content:encoded><![CDATA[<p>Willie Wheeler, author of &#8216;Spring in Practice&#8217; has a name for the second concern I have with catching Exception: &#8216;Mask via broad catch, and misreport&#8217; (see Wheeler&#8217;s answer to StackOverflow question: <a href="http://stackoverflow.com/questions/6591470/checked-vs-unchecked-exceptions-in-service-layer" rel="nofollow">http://stackoverflow.com/questions/6591470/checked-vs-unchecked-exceptions-in-service-layer</a>).</p>
<p>Philip</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: Philip Schwarz				</title>
				<link>http://matteo.vaccari.name/blog/archives/875/comment-page-1#comment-96407</link>
		<dc:creator><![CDATA[Philip Schwarz]]></dc:creator>
		<pubDate>Sat, 30 Nov 2013 12:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=875#comment-96407</guid>
					<description><![CDATA[&#062;RuntimeExceptions might happen for all kinds of reasons, like NullPointerExceptions which are certainly due to logic errors, but also OutOfMemoryError or even applicative errors 

No RuntimException can happen due to OutOfMemoryError because the latter extends Error:

java.lang.Object &#060;== java.lang.Throwable  &#060;== java.lang.Error &#060;==  java.lang.VirtualMachineError &#060;== java.lang.OutOfMemoryError.

So when you catch Exception, you run no risk of catching any Error.

While RuntimeException should not normally be caught because it indicates a programming error, Error should not normally be caught because it is a serious problem and most of the time and  abnormal condition.

Philip]]></description>
		<content:encoded><![CDATA[<p>&gt;RuntimeExceptions might happen for all kinds of reasons, like NullPointerExceptions which are certainly due to logic errors, but also OutOfMemoryError or even applicative errors </p>
<p>No RuntimException can happen due to OutOfMemoryError because the latter extends Error:</p>
<p>java.lang.Object &lt;== java.lang.Throwable  &lt;== java.lang.Error &lt;==  java.lang.VirtualMachineError &lt;== java.lang.OutOfMemoryError.</p>
<p>So when you catch Exception, you run no risk of catching any Error.</p>
<p>While RuntimeException should not normally be caught because it indicates a programming error, Error should not normally be caught because it is a serious problem and most of the time and  abnormal condition.</p>
<p>Philip</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: Uberto				</title>
				<link>http://matteo.vaccari.name/blog/archives/875/comment-page-1#comment-96365</link>
		<dc:creator><![CDATA[Uberto]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 16:59:01 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=875#comment-96365</guid>
					<description><![CDATA[Hi Matteo, while I certainly agree with you here, I think you are considering only the &quot;easy&quot; case.
In all examples here, exception are raised for &quot;exceptional&quot; cases, either bugs in our code or a failure of our environment (mail server down). 
I&#039;ll wait until you wrote something about exceptions used for flow control.  (http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl)

:)]]></description>
		<content:encoded><![CDATA[<p>Hi Matteo, while I certainly agree with you here, I think you are considering only the &#8220;easy&#8221; case.<br />
In all examples here, exception are raised for &#8220;exceptional&#8221; cases, either bugs in our code or a failure of our environment (mail server down).<br />
I&#8217;ll wait until you wrote something about exceptions used for flow control.  (<a href="http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl" rel="nofollow">http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl</a>)</p>
<p>:)</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: matteo				</title>
				<link>http://matteo.vaccari.name/blog/archives/875/comment-page-1#comment-96359</link>
		<dc:creator><![CDATA[matteo]]></dc:creator>
		<pubDate>Mon, 25 Nov 2013 09:25:56 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=875#comment-96359</guid>
					<description><![CDATA[Hello Philip,

thanks for your comment.  I don&#039;t share your concern about catching Exception.  RuntimeExceptions might happen for all kinds of reasons, like NullPointerExceptions which are certainly due to logic errors, but also OutOfMemoryError or even applicative errors that the developer chose to declare as unchecked exceptions.  

I think they should be handled uniformly; whenever an exception is thrown, the fact is that we cannot do what the user asked us to do.  We log appropriately and present an appropriate error message to the user.  Even logic errors are a fact of life; I want the system to handle them as gracefully as possible.

Exceptions should be exceptional; for instance in my example above, there should probably be a check that the string actually contains a parseable integer, so that no ParseException is ever thrown.  Most exceptions should be prevented by checking that an operation can be performed before we perform it.  If we do this, then anything that is thrown should either be something that we didn&#039;t think of, like a hole in our validation checks, or something that we cannot possibly do anything about, like our database server is unreachable or we&#039;re out of memory.  I don&#039;t think you can gain much by catching exceptions selectively then.

By the way: the pattern that I called the &quot;Fundamental Pattern of Exception Handling&quot; is not something I invented, of course.  I learned about it when I worked with the MacApp application framework, circa 1990 :-)


]]></description>
		<content:encoded><![CDATA[<p>Hello Philip,</p>
<p>thanks for your comment.  I don&#8217;t share your concern about catching Exception.  RuntimeExceptions might happen for all kinds of reasons, like NullPointerExceptions which are certainly due to logic errors, but also OutOfMemoryError or even applicative errors that the developer chose to declare as unchecked exceptions.  </p>
<p>I think they should be handled uniformly; whenever an exception is thrown, the fact is that we cannot do what the user asked us to do.  We log appropriately and present an appropriate error message to the user.  Even logic errors are a fact of life; I want the system to handle them as gracefully as possible.</p>
<p>Exceptions should be exceptional; for instance in my example above, there should probably be a check that the string actually contains a parseable integer, so that no ParseException is ever thrown.  Most exceptions should be prevented by checking that an operation can be performed before we perform it.  If we do this, then anything that is thrown should either be something that we didn&#8217;t think of, like a hole in our validation checks, or something that we cannot possibly do anything about, like our database server is unreachable or we&#8217;re out of memory.  I don&#8217;t think you can gain much by catching exceptions selectively then.</p>
<p>By the way: the pattern that I called the &#8220;Fundamental Pattern of Exception Handling&#8221; is not something I invented, of course.  I learned about it when I worked with the MacApp application framework, circa 1990 :-)</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: Philip Schwarz				</title>
				<link>http://matteo.vaccari.name/blog/archives/875/comment-page-1#comment-96356</link>
		<dc:creator><![CDATA[Philip Schwarz]]></dc:creator>
		<pubDate>Sun, 24 Nov 2013 07:07:15 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=875#comment-96356</guid>
					<description><![CDATA[Hi Matteo. 

Interesting post, especially the usage of the publish-subscribe pattern to eliminate the tight coupling between tripling the number and sending the email.

I have a question about the following though:

void onTripleButtonPressed() {
  try {
    tryOnTripleButtonPressed();
  } catch (Exception e) {
    alertUser(e.getMessage());
  }
}

Normally when I see code catching java.lang.Exception, I bring up the following two reasons why I think it should not be done.

The first reason is explained in the Catching Exception antipattern: 
https://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html#catchingException

My other reason why catching java.lang.Exception (or java.lang.Throwable for that matter) is an antipattern is that one of the subclasses of Exception is RuntimeException (http://javajazzle.files.wordpress.com/2011/02/hierarchy_of_java_exceptions.png).  So whenever you catch Exception, you are also catching RuntimeException. But runtime exceptions are generally supposed to indicate programming errors. 

A correctly working system is generally not expected to encounter runtime exceptions. When a system encounters a runtime exception, you wan to be sure that the system informs you about it, and you then want to go and fix the programming error that caused it. 

There is no point in handling a runtime exception programmatically (other than reporting it) because if it is ever encountered, you will fix the error that caused it, so it should never be raised again. If you catch Exception, you run the risk of handling runtime exceptions in the same way as checked exceptions, which is almost certainly incorrect. 

So if you really have to catch Exception (this is highly discouraged), you should first catch RuntimeException and rethrow it:
 
catch(RuntimeException re)
{
    throw re
}
catch(Exception e)
{
    ...some logic...
}

Of course in most systems that are in production, instead of allowing a runtime exception to bubble all the way up, which would bring the system to a halt, we want to catch them at the top of our exception handling structure, and inform interested parties about it.

If we apply the Fundamental Pattern of Exception Handling, there is only one point where exceptions are handled, so it makes sense to catch runtime exceptions as well, but we want to handle them differently from checked exceptions. I guess pointing this out was out of scope for your post.

What are your thoughts on the two reasons I mentioned for not catching Exception?]]></description>
		<content:encoded><![CDATA[<p>Hi Matteo. </p>
<p>Interesting post, especially the usage of the publish-subscribe pattern to eliminate the tight coupling between tripling the number and sending the email.</p>
<p>I have a question about the following though:</p>
<p>void onTripleButtonPressed() {<br />
  try {<br />
    tryOnTripleButtonPressed();<br />
  } catch (Exception e) {<br />
    alertUser(e.getMessage());<br />
  }<br />
}</p>
<p>Normally when I see code catching java.lang.Exception, I bring up the following two reasons why I think it should not be done.</p>
<p>The first reason is explained in the Catching Exception antipattern:<br />
<a href="https://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html#catchingException" rel="nofollow">https://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html#catchingException</a></p>
<p>My other reason why catching java.lang.Exception (or java.lang.Throwable for that matter) is an antipattern is that one of the subclasses of Exception is RuntimeException (<a href="http://javajazzle.files.wordpress.com/2011/02/hierarchy_of_java_exceptions.png" rel="nofollow">http://javajazzle.files.wordpress.com/2011/02/hierarchy_of_java_exceptions.png</a>).  So whenever you catch Exception, you are also catching RuntimeException. But runtime exceptions are generally supposed to indicate programming errors. </p>
<p>A correctly working system is generally not expected to encounter runtime exceptions. When a system encounters a runtime exception, you wan to be sure that the system informs you about it, and you then want to go and fix the programming error that caused it. </p>
<p>There is no point in handling a runtime exception programmatically (other than reporting it) because if it is ever encountered, you will fix the error that caused it, so it should never be raised again. If you catch Exception, you run the risk of handling runtime exceptions in the same way as checked exceptions, which is almost certainly incorrect. </p>
<p>So if you really have to catch Exception (this is highly discouraged), you should first catch RuntimeException and rethrow it:</p>
<p>catch(RuntimeException re)<br />
{<br />
    throw re<br />
}<br />
catch(Exception e)<br />
{<br />
    &#8230;some logic&#8230;<br />
}</p>
<p>Of course in most systems that are in production, instead of allowing a runtime exception to bubble all the way up, which would bring the system to a halt, we want to catch them at the top of our exception handling structure, and inform interested parties about it.</p>
<p>If we apply the Fundamental Pattern of Exception Handling, there is only one point where exceptions are handled, so it makes sense to catch runtime exceptions as well, but we want to handle them differently from checked exceptions. I guess pointing this out was out of scope for your post.</p>
<p>What are your thoughts on the two reasons I mentioned for not catching Exception?</p>
]]></content:encoded>
						</item>
			</channel>
</rss>
