<?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: When To Use Exceptions</title>
	<atom:link href="http://barelyenough.org/blog/2007/11/when-to-use-exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/</link>
	<description>… and there is much to be learned</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:46:19 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: &#8220;拥抱&#8221;异常，还是，&#8220;固守&#8221;返回值？ &#124; DCCMX - Performance &#38; Security</title>
		<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/comment-page-1/#comment-85753</link>
		<dc:creator>&#8220;拥抱&#8221;异常，还是，&#8220;固守&#8221;返回值？ &#124; DCCMX - Performance &#38; Security</dc:creator>
		<pubDate>Mon, 14 Nov 2011 04:14:47 +0000</pubDate>
		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/11/when-to-use-exceptions/#comment-85753</guid>
		<description>[...] When To Use Exceptions http://barelyenough.org/blog/2007/11/when-to-use-exceptions/ [...]</description>
		<content:encoded><![CDATA[<p>[...] When To Use Exceptions <a href="http://barelyenough.org/blog/2007/11/when-to-use-exceptions/" rel="nofollow">http://barelyenough.org/blog/2007/11/when-to-use-exceptions/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tech Per</title>
		<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/comment-page-1/#comment-39605</link>
		<dc:creator>Tech Per</dc:creator>
		<pubDate>Mon, 26 Nov 2007 21:18:34 +0000</pubDate>
		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/11/when-to-use-exceptions/#comment-39605</guid>
		<description>&lt;p&gt;Hi Peter,&lt;/p&gt;

&lt;p&gt;I think you are right in one thing: That it all comes down to defining what an exceptional situation is. And, maybe, this is also what makes exceptions hard to use, and quite debated. Some even find that they have no place in a programming language. I guess this is also why I think we won&#039;t agree on this subject :-)&lt;/p&gt;

&lt;p&gt;Now, about your example with the coffee shop. Hmm, well a bit far-fetched, I think but okay. If the mocha-master is telling you &quot;sorry we are all out of dark coffee, do you want to wait while we brew more?&quot;, this should then be directly transferable to the Map.get(&quot;foo&quot;) call saying &#039;sorry, I am all out of values on key &quot;foo&quot;, do you want to wait for it to appear somehow, from somewhere?&quot; :-)&lt;/p&gt;

&lt;p&gt;Happy hacking in Ruby. Now &lt;em&gt;that&#039;s&lt;/em&gt; fun !!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Peter,</p>
<p>I think you are right in one thing: That it all comes down to defining what an exceptional situation is. And, maybe, this is also what makes exceptions hard to use, and quite debated. Some even find that they have no place in a programming language. I guess this is also why I think we won&#8217;t agree on this subject :-)</p>
<p>Now, about your example with the coffee shop. Hmm, well a bit far-fetched, I think but okay. If the mocha-master is telling you &#8220;sorry we are all out of dark coffee, do you want to wait while we brew more?&#8221;, this should then be directly transferable to the Map.get(&#8220;foo&#8221;) call saying &#8216;sorry, I am all out of values on key &#8220;foo&#8221;, do you want to wait for it to appear somehow, from somewhere?&#8221; :-)</p>
<p>Happy hacking in Ruby. Now <em>that&#8217;s</em> fun !!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Williams</title>
		<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/comment-page-1/#comment-39456</link>
		<dc:creator>Peter Williams</dc:creator>
		<pubDate>Mon, 26 Nov 2007 04:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/11/when-to-use-exceptions/#comment-39456</guid>
		<description>&lt;p&gt;Dan,&lt;/p&gt;

&lt;p&gt;It all comes down to defining &quot;exception circumstance&quot;.  I don&#039;t think
you can reasonably define that from the application&#039;s point of view,
because that would require a deep understanding of all possible
applications.  If you accept that the only thing you are left with is
to use the library&#039;s perspective, which turns out to be reasonably
tractable.&lt;/p&gt;

&lt;p&gt;But from that perspective you end up raising a lot more exceptions
because a library does not know, or care, if an application is
going to be able to operate without a configuration item that is
missing or not, it only knows that the application asked for a
configuration item and it is not able to provide that item.  It is an
exceptional circumstance because the application would not have asked
for something it did not care about.&lt;/p&gt;

&lt;p&gt;As for messy syntax and inefficiency, I suggest that you never let
perceived, or suspected, inefficiencies in the compiler or runtime
impact coding decisions until you have profiles in hand showing that
it actually matters.  It almost never does.&lt;/p&gt;

&lt;p&gt;Syntax, though is a different issue.  But you should think carefully
before you trade &lt;code&gt;try{...} catch{...}&lt;/code&gt; for &lt;code&gt;...; if(){}&lt;/code&gt;.  There
probably are situations where the second one is much better than the
first but more often than not you are just bring the uncommon/error
path into the main line flow of your code.  That usually makes it
harder to understand the intent of the code.  Moving the handling
exceptional circumstances (even those that are allowed, or even
expected some of the time, like a missing configuration item) out into
a catch block can have the effect of making the happy path a &lt;em&gt;lot&lt;/em&gt;
easier to understand.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Dan,</p>
<p>It all comes down to defining &#8220;exception circumstance&#8221;.  I don&#8217;t think<br />
you can reasonably define that from the application&#8217;s point of view,<br />
because that would require a deep understanding of all possible<br />
applications.  If you accept that the only thing you are left with is<br />
to use the library&#8217;s perspective, which turns out to be reasonably<br />
tractable.</p>
<p>But from that perspective you end up raising a lot more exceptions<br />
because a library does not know, or care, if an application is<br />
going to be able to operate without a configuration item that is<br />
missing or not, it only knows that the application asked for a<br />
configuration item and it is not able to provide that item.  It is an<br />
exceptional circumstance because the application would not have asked<br />
for something it did not care about.</p>
<p>As for messy syntax and inefficiency, I suggest that you never let<br />
perceived, or suspected, inefficiencies in the compiler or runtime<br />
impact coding decisions until you have profiles in hand showing that<br />
it actually matters.  It almost never does.</p>
<p>Syntax, though is a different issue.  But you should think carefully<br />
before you trade <code>try{...} catch{...}</code> for <code>...; if(){}</code>.  There<br />
probably are situations where the second one is much better than the<br />
first but more often than not you are just bring the uncommon/error<br />
path into the main line flow of your code.  That usually makes it<br />
harder to understand the intent of the code.  Moving the handling<br />
exceptional circumstances (even those that are allowed, or even<br />
expected some of the time, like a missing configuration item) out into<br />
a catch block can have the effect of making the happy path a <em>lot</em><br />
easier to understand.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Williams</title>
		<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/comment-page-1/#comment-39406</link>
		<dc:creator>Peter Williams</dc:creator>
		<pubDate>Sun, 25 Nov 2007 22:14:15 +0000</pubDate>
		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/11/when-to-use-exceptions/#comment-39406</guid>
		<description>&lt;p&gt;Tech Per,&lt;/p&gt;

&lt;p&gt;In one sense you are certainly correct that a method should do what
it&#039;s documentation says it does.  However, I consider that to be a bit
of dodge.  Merely documenting the behavior of a poorly thought out
method does not somehow make it good (though that may be the only
viable option for many existing methods).&lt;/p&gt;

&lt;p&gt;Most languages don&#039;t good semantics for defining contracts, but that
is OK (maybe even good).  We get along just fine in the real world
with out explicit, bullet proof, contracts.  For example, when I go
into my local coffee shop and say &quot;give me a cup of dark coffee&quot; I
don&#039;t have an explicit written contract with the person behind the
counter but I would be really annoyed if they just handed me an empty
cup.  Of course they don&#039;t do that, they say &quot;sorry we are all out of
dark coffee, do you want to wait while we brew more?  That is, they
raise an exception and let me decide what to do with it.&lt;/p&gt;

&lt;p&gt;There are a couple of possible definitions of &quot;an exceptional
situation&quot;.  I define it as a method not being able to do what the
caller asked.  Defining &quot;an exceptional situation&quot; any other way means
that the library author really has to guessing about how the library
will be used, and that never works out well.&lt;/p&gt;

&lt;p&gt;It ought to be fairly clear from the method name what is being asked
(that is what we mean by &quot;meaningful names&quot;).  A method like &lt;code&gt;get&lt;/code&gt;
should not be defined by the comments to mean &quot;return the request
thing unless it rhythms with shoe in which case append &#039;... in bed&#039; to
it and return that&quot;.  No, we know that when we say &quot;get&quot; we mean &quot;go
find the thing I am asking about and give it to me&quot;.  If the method
cannot do that it should let the caller know that fact so that it can
deal with it appropriately.&lt;/p&gt;

&lt;p&gt;If you need method that gets but appends &#039;... in bed&#039; to the value (or
returns some default value when the desired thing does not exist) by
all means have one of those, but be sure to name it descriptively, so
that every one knows what they are getting into.&lt;/p&gt;

&lt;p&gt;I do have so say that I find your first example a bit clearer than the
second.  However, I think what you really want is a &quot;does a particular
bean exist&quot; predicate, e.g.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if beanHome.beanWithPKExists(PkOfInterest) {
  bean = beanHome.findByPK(PkOfInterest);
} else {
  bean = ...&#039; // create bean here 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then probably put block of code in a method like
&lt;code&gt;findOrCreateFooBean()&lt;/code&gt;.  On the other hand if you have such a method
the first form would be pretty good.&lt;/p&gt;

&lt;p&gt;As for annotations, I am not actively writing any Java at this point.
I have in the past and may again in the future but currently I am
using mostly Ruby.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Tech Per,</p>
<p>In one sense you are certainly correct that a method should do what<br />
it&#8217;s documentation says it does.  However, I consider that to be a bit<br />
of dodge.  Merely documenting the behavior of a poorly thought out<br />
method does not somehow make it good (though that may be the only<br />
viable option for many existing methods).</p>
<p>Most languages don&#8217;t good semantics for defining contracts, but that<br />
is OK (maybe even good).  We get along just fine in the real world<br />
with out explicit, bullet proof, contracts.  For example, when I go<br />
into my local coffee shop and say &#8220;give me a cup of dark coffee&#8221; I<br />
don&#8217;t have an explicit written contract with the person behind the<br />
counter but I would be really annoyed if they just handed me an empty<br />
cup.  Of course they don&#8217;t do that, they say &#8220;sorry we are all out of<br />
dark coffee, do you want to wait while we brew more?  That is, they<br />
raise an exception and let me decide what to do with it.</p>
<p>There are a couple of possible definitions of &#8220;an exceptional<br />
situation&#8221;.  I define it as a method not being able to do what the<br />
caller asked.  Defining &#8220;an exceptional situation&#8221; any other way means<br />
that the library author really has to guessing about how the library<br />
will be used, and that never works out well.</p>
<p>It ought to be fairly clear from the method name what is being asked<br />
(that is what we mean by &#8220;meaningful names&#8221;).  A method like <code>get</code><br />
should not be defined by the comments to mean &#8220;return the request<br />
thing unless it rhythms with shoe in which case append &#8216;&#8230; in bed&#8217; to<br />
it and return that&#8221;.  No, we know that when we say &#8220;get&#8221; we mean &#8220;go<br />
find the thing I am asking about and give it to me&#8221;.  If the method<br />
cannot do that it should let the caller know that fact so that it can<br />
deal with it appropriately.</p>
<p>If you need method that gets but appends &#8216;&#8230; in bed&#8217; to the value (or<br />
returns some default value when the desired thing does not exist) by<br />
all means have one of those, but be sure to name it descriptively, so<br />
that every one knows what they are getting into.</p>
<p>I do have so say that I find your first example a bit clearer than the<br />
second.  However, I think what you really want is a &#8220;does a particular<br />
bean exist&#8221; predicate, e.g.</p>
<pre><code>if beanHome.beanWithPKExists(PkOfInterest) {
  bean = beanHome.findByPK(PkOfInterest);
} else {
  bean = ...' // create bean here
}
</code></pre>
<p>And then probably put block of code in a method like<br />
<code>findOrCreateFooBean()</code>.  On the other hand if you have such a method<br />
the first form would be pretty good.</p>
<p>As for annotations, I am not actively writing any Java at this point.<br />
I have in the past and may again in the future but currently I am<br />
using mostly Ruby.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/comment-page-1/#comment-39393</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Sun, 25 Nov 2007 18:07:00 +0000</pubDate>
		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/11/when-to-use-exceptions/#comment-39393</guid>
		<description>&lt;p&gt;The environment may have something to do with the best approach. In Java, dealing with exceptions is a messy pain in the arse. There is also a lot of overhead associated with creating and throwing exceptions; because exceptions are supposed to be unusual, they are not optimized for.&lt;/p&gt;

&lt;p&gt;Using exceptions for unexceptional circumstances is considered an anti-pattern in Java. I would much rather have Map.get(&quot;key&quot;) return an ambiguous null and, if the distinction matters to me, use an idiom like if(!map.contains(&quot;key&quot;)) ... else value = map.get(&quot;key&quot;); if(value == null) ... than be forced to wrap every single Map access in my code with a try/catch block.&lt;/p&gt;

&lt;p&gt;Your example may make sense in Ruby (I don&#039;t know Ruby well enough to have an opinion.) It doesn&#039;t make sense in Java. Or perhaps the example was just poorly chosen?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The environment may have something to do with the best approach. In Java, dealing with exceptions is a messy pain in the arse. There is also a lot of overhead associated with creating and throwing exceptions; because exceptions are supposed to be unusual, they are not optimized for.</p>
<p>Using exceptions for unexceptional circumstances is considered an anti-pattern in Java. I would much rather have Map.get(&#8220;key&#8221;) return an ambiguous null and, if the distinction matters to me, use an idiom like if(!map.contains(&#8220;key&#8221;)) &#8230; else value = map.get(&#8220;key&#8221;); if(value == null) &#8230; than be forced to wrap every single Map access in my code with a try/catch block.</p>
<p>Your example may make sense in Ruby (I don&#8217;t know Ruby well enough to have an opinion.) It doesn&#8217;t make sense in Java. Or perhaps the example was just poorly chosen?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tech Per</title>
		<link>http://barelyenough.org/blog/2007/11/when-to-use-exceptions/comment-page-1/#comment-39343</link>
		<dc:creator>Tech Per</dc:creator>
		<pubDate>Sun, 25 Nov 2007 12:25:34 +0000</pubDate>
		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/11/when-to-use-exceptions/#comment-39343</guid>
		<description>&lt;p&gt;Hmm, I seem to disagree a bit with you and agree a bit more with Cedric. I am not sure, though, cause when you say:&lt;/p&gt;

&lt;p&gt;&quot;....Either way my advice is: Do not use nil as a way to indicate that the object was unable to perform the requested operation, that is job of exceptions.....&quot;&lt;/p&gt;

&lt;p&gt;I can agree. IFF &quot;..the object was unable to perform the requested operation..&quot; means that the contract cannot be fulfilled. Now, in Java, we cannot always express all we want about contracts, as for example Eiffel can. But, we can do it in javadoc or sometimes by annotations. If, the method is documented to return null if nothing is found on a key lookup in a dictionary (to take your example), then by all means, returning null is the right thing to do. It is NOT exceptional, that the key doesn&#039;t exist or the value is null. In Java, the Map class includes an contains method too.&lt;/p&gt;

&lt;p&gt;Now, I then get confused about what you really mean, when you write:&lt;/p&gt;

&lt;p&gt;&quot;....Exceptions are not about “exceptional situations”, whatever that means. They are a way for methods tell their caller that it was not able to do what the caller asked it to do. If I say, “dictionary give me the value associated with a particular key” and the key does not exist, the appropriate response is a NoSuchKey exception. Returning a nil in that situation is a lie....&quot;&lt;/p&gt;

&lt;p&gt;Cause hell yeahh, exceptions are about exceptional conditions only!! There are just not many programmers that recoqnize this, I think. Even Sun can make mistakes. I have always hated this construct from JEE:&lt;/p&gt;

&lt;p&gt;try {
  bean = beanHome.findByPK(...);
  // update bean here
} catch (ObjectNotFoundException e) {
  // create bean here
}&lt;/p&gt;

&lt;p&gt;That sucks. I have control logic (is this a create or an update) done with exceptions. Bwadrh! This should be a:&lt;/p&gt;

&lt;p&gt;bean = beanHome.findByPK(...);
if (bean == null) {
  // create
} else {
  // update
}&lt;/p&gt;

&lt;p&gt;Have you considered using @Null/@NotNull annotations on methods together with a IDE that can use them for anything?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hmm, I seem to disagree a bit with you and agree a bit more with Cedric. I am not sure, though, cause when you say:</p>
<p>&#8220;&#8230;.Either way my advice is: Do not use nil as a way to indicate that the object was unable to perform the requested operation, that is job of exceptions&#8230;..&#8221;</p>
<p>I can agree. IFF &#8220;..the object was unable to perform the requested operation..&#8221; means that the contract cannot be fulfilled. Now, in Java, we cannot always express all we want about contracts, as for example Eiffel can. But, we can do it in javadoc or sometimes by annotations. If, the method is documented to return null if nothing is found on a key lookup in a dictionary (to take your example), then by all means, returning null is the right thing to do. It is NOT exceptional, that the key doesn&#8217;t exist or the value is null. In Java, the Map class includes an contains method too.</p>
<p>Now, I then get confused about what you really mean, when you write:</p>
<p>&#8220;&#8230;.Exceptions are not about “exceptional situations”, whatever that means. They are a way for methods tell their caller that it was not able to do what the caller asked it to do. If I say, “dictionary give me the value associated with a particular key” and the key does not exist, the appropriate response is a NoSuchKey exception. Returning a nil in that situation is a lie&#8230;.&#8221;</p>
<p>Cause hell yeahh, exceptions are about exceptional conditions only!! There are just not many programmers that recoqnize this, I think. Even Sun can make mistakes. I have always hated this construct from JEE:</p>
<p>try {<br />
  bean = beanHome.findByPK(&#8230;);<br />
  // update bean here<br />
} catch (ObjectNotFoundException e) {<br />
  // create bean here<br />
}</p>
<p>That sucks. I have control logic (is this a create or an update) done with exceptions. Bwadrh! This should be a:</p>
<p>bean = beanHome.findByPK(&#8230;);<br />
if (bean == null) {<br />
  // create<br />
} else {<br />
  // update<br />
}</p>
<p>Have you considered using @Null/@NotNull annotations on methods together with a IDE that can use them for anything?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

