<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Exceptions, you&#8217;re doing it wrong</title>
	<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/</link>
	<description>Mr Quinn, one word for your fans? "Ehm, astronaut."</description>
	<pubDate>Fri, 21 Nov 2008 03:29:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: Kadam Ganesh</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-758</link>
		<dc:creator>Kadam Ganesh</dc:creator>
		<pubDate>Thu, 20 Nov 2008 05:16:47 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-758</guid>
		<description>Overall a nice article..As suggested by Karl, it is not a very good idea to write 
exceptions for each and every error case. What i would like to know is  - anybody has any idea about creating a Filter in symfony which will track error mesage and code for each request made. So that errors can be known and dealt with accordingly in the code..</description>
		<content:encoded><![CDATA[<p>Overall a nice article..As suggested by Karl, it is not a very good idea to write<br />
exceptions for each and every error case. What i would like to know is  - anybody has any idea about creating a Filter in symfony which will track error mesage and code for each request made. So that errors can be known and dealt with accordingly in the code..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Thong</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-543</link>
		<dc:creator>Joe Thong</dc:creator>
		<pubDate>Thu, 17 Jul 2008 10:14:10 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-543</guid>
		<description>We have a similar problem a while ago.  PropelException was thrown when we are trying to add a duplicate record.  I think checking if the duplicate exists before inserting is a bad idea since I don't want to spend so much SELECT resources just to prevent the 0.05% occurence of a duplicate record in the table.  At first I was stumped since PropelException is too generic.  Well luckily what Propel did was they actually embedded the PDOException raised by PDO in their PropelException class and could be accessed via the getCause() method.  I would then test if this embedded exception is a PDOException and if the PDOException code equals to the 23000 SQLSTATE error code for duplicate records.  

The code is: 

$e-&#62;getCause() instanceof PDOException &#38;&#38; 23000 === (int) $e-&#62;getCause()-&#62;getCode()</description>
		<content:encoded><![CDATA[<p>We have a similar problem a while ago.  PropelException was thrown when we are trying to add a duplicate record.  I think checking if the duplicate exists before inserting is a bad idea since I don&#8217;t want to spend so much SELECT resources just to prevent the 0.05% occurence of a duplicate record in the table.  At first I was stumped since PropelException is too generic.  Well luckily what Propel did was they actually embedded the PDOException raised by PDO in their PropelException class and could be accessed via the getCause() method.  I would then test if this embedded exception is a PDOException and if the PDOException code equals to the 23000 SQLSTATE error code for duplicate records.  </p>
<p>The code is: </p>
<p>$e-&gt;getCause() instanceof PDOException &amp;&amp; 23000 === (int) $e-&gt;getCause()-&gt;getCode()</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rpgfan3233</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-521</link>
		<dc:creator>rpgfan3233</dc:creator>
		<pubDate>Mon, 16 Jun 2008 17:03:10 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-521</guid>
		<description>The biggest problem I have with exceptions is when to catch them and provide a friendly message for users v.s. when to let the exception speak for itself v.s. when to catch, repackage and raise the exception again. For example, I was playing with some libxml2/libxslt Python bindings a few weeks ago when I found that I was using try-except far too much because I played with XML schema, XSLT and XML all at once, and I parsed each file to make sure it was well-formed XML before trying to actually use it. Coming from C, I was more worried about specific error messages and return codes than letting things degrade gracefully. After all, letting Python say "parseError" rather than catching the exception is far more efficient, and it doesn't really harm anything because of the fact that every exception is fatal. How would I know which file failed with a parseError? Debugging, of course. ^_^

Great article overall. Sometimes it is too easy to get happy about using exceptions and classes rather than simple errors and global functions. I once saw some code for a PHP style sheet switcher that was created using a classes... Talk about overkill! :P</description>
		<content:encoded><![CDATA[<p>The biggest problem I have with exceptions is when to catch them and provide a friendly message for users v.s. when to let the exception speak for itself v.s. when to catch, repackage and raise the exception again. For example, I was playing with some libxml2/libxslt Python bindings a few weeks ago when I found that I was using try-except far too much because I played with XML schema, XSLT and XML all at once, and I parsed each file to make sure it was well-formed XML before trying to actually use it. Coming from C, I was more worried about specific error messages and return codes than letting things degrade gracefully. After all, letting Python say &#8220;parseError&#8221; rather than catching the exception is far more efficient, and it doesn&#8217;t really harm anything because of the fact that every exception is fatal. How would I know which file failed with a parseError? Debugging, of course. ^_^</p>
<p>Great article overall. Sometimes it is too easy to get happy about using exceptions and classes rather than simple errors and global functions. I once saw some code for a PHP style sheet switcher that was created using a classes&#8230; Talk about overkill! :P</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phloidster</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-516</link>
		<dc:creator>phloidster</dc:creator>
		<pubDate>Mon, 16 Jun 2008 03:39:41 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-516</guid>
		<description>Actually, you're only half right:  almost none of these examples should be handled by exceptions at all.

The real abuse of exceptions is using them for domain logic.

There is nothing 'exceptional' about not being able to connect to a database, find a row in a table, etc.

These are all situations which fall into the expected logical space of possibilities.

The fact that many frameworks use exceptions incorrectly does not make it acceptable.

This type of exception abuse is part of the downward spiral of ever poorer quality code these days.

Execptions are a mechanism to escape multile levels of a call stack for situations which are so, well, exceptional, that they mostly do not happen.

ALL other use of exceptions is pathlogical and evidence of mental laziness.</description>
		<content:encoded><![CDATA[<p>Actually, you&#8217;re only half right:  almost none of these examples should be handled by exceptions at all.</p>
<p>The real abuse of exceptions is using them for domain logic.</p>
<p>There is nothing &#8216;exceptional&#8217; about not being able to connect to a database, find a row in a table, etc.</p>
<p>These are all situations which fall into the expected logical space of possibilities.</p>
<p>The fact that many frameworks use exceptions incorrectly does not make it acceptable.</p>
<p>This type of exception abuse is part of the downward spiral of ever poorer quality code these days.</p>
<p>Execptions are a mechanism to escape multile levels of a call stack for situations which are so, well, exceptional, that they mostly do not happen.</p>
<p>ALL other use of exceptions is pathlogical and evidence of mental laziness.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George M Jempty</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-285</link>
		<dc:creator>George M Jempty</dc:creator>
		<pubDate>Fri, 23 May 2008 21:43:56 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-285</guid>
		<description>The reason they all treat an exception from the database layer as a fatal exception, is that they all make the erroneous assumption that "model" and "database" are synonymous.</description>
		<content:encoded><![CDATA[<p>The reason they all treat an exception from the database layer as a fatal exception, is that they all make the erroneous assumption that &#8220;model&#8221; and &#8220;database&#8221; are synonymous.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Reich</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-282</link>
		<dc:creator>Brian Reich</dc:creator>
		<pubDate>Fri, 23 May 2008 18:54:43 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-282</guid>
		<description>Jon is absolutely right.  The base Exception class in PHP takes a message and an error code for a reason, and I think a lot of people forget that.  In fact it seems that my all-time favorite framework, the Zend Framework, even forgets this and passes only an error code in many of it's libraries.

I think subclassing your Exceptions just makes sense.  A database connection error might be fatal, but a DB Query error isn't neccessarily treated the same way. Of course creating a unique Exception subclass for each of the ways that a DB query can fail would just be ridiculous, so at some point it's up to the programmer to recognize when it's time to customize a class via parameters rather than subclassing.

What the argument boils down to?  Wether you want to handle this way:

catch(DBConnectionException $dbce) {
    ...
} catch(DBException $dbe) {
    ...
} catch(Exception $e) {
    ...
}

Or this way:

catch(Exception $e) {
    if($e-&#62;getCode() == DBException::CONNECTION_EXCEPTION) {
        ....
    } else {
        ....
    }
}</description>
		<content:encoded><![CDATA[<p>Jon is absolutely right.  The base Exception class in PHP takes a message and an error code for a reason, and I think a lot of people forget that.  In fact it seems that my all-time favorite framework, the Zend Framework, even forgets this and passes only an error code in many of it&#8217;s libraries.</p>
<p>I think subclassing your Exceptions just makes sense.  A database connection error might be fatal, but a DB Query error isn&#8217;t neccessarily treated the same way. Of course creating a unique Exception subclass for each of the ways that a DB query can fail would just be ridiculous, so at some point it&#8217;s up to the programmer to recognize when it&#8217;s time to customize a class via parameters rather than subclassing.</p>
<p>What the argument boils down to?  Wether you want to handle this way:</p>
<p>catch(DBConnectionException $dbce) {<br />
    &#8230;<br />
} catch(DBException $dbe) {<br />
    &#8230;<br />
} catch(Exception $e) {<br />
    &#8230;<br />
}</p>
<p>Or this way:</p>
<p>catch(Exception $e) {<br />
    if($e-&gt;getCode() == DBException::CONNECTION_EXCEPTION) {<br />
        &#8230;.<br />
    } else {<br />
        &#8230;.<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fredrik Holmström</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-271</link>
		<dc:creator>Fredrik Holmström</dc:creator>
		<pubDate>Fri, 23 May 2008 18:16:02 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-271</guid>
		<description>Eli: Nice trick, but all in all I have to say that Python is pretty good at doing "the right thing" with exceptions, KeyError, IterationDone (or whatever the one that ends an iterator is called, always forget), ValueError, etc.</description>
		<content:encoded><![CDATA[<p>Eli: Nice trick, but all in all I have to say that Python is pretty good at doing &#8220;the right thing&#8221; with exceptions, KeyError, IterationDone (or whatever the one that ends an iterator is called, always forget), ValueError, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eli Collins</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-270</link>
		<dc:creator>Eli Collins</dc:creator>
		<pubDate>Fri, 23 May 2008 18:10:52 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-270</guid>
		<description>Spot on, and I agree that this problem occurs in multiple languages.
Python's DBAPI uses the exact same generic DatabaseError style.
It annoyed me enough that I wrote a translation layer to catch DatabaseError, run it through some regexps/rules in an external library, and then my apps
can rely on specified exceptions...

sorry this is python not php, but the pattern still works :)

try:
    try:
        ...
    except DatabaseError, err:
        raise sdutil.adapt_db_error(err)
except sdutil.ConnectionClosedError:
   ...

found this pattern useful, as it lets the regexp hacks stay in an independantly maintained library.</description>
		<content:encoded><![CDATA[<p>Spot on, and I agree that this problem occurs in multiple languages.<br />
Python&#8217;s DBAPI uses the exact same generic DatabaseError style.<br />
It annoyed me enough that I wrote a translation layer to catch DatabaseError, run it through some regexps/rules in an external library, and then my apps<br />
can rely on specified exceptions&#8230;</p>
<p>sorry this is python not php, but the pattern still works :)</p>
<p>try:<br />
    try:<br />
        &#8230;<br />
    except DatabaseError, err:<br />
        raise sdutil.adapt_db_error(err)<br />
except sdutil.ConnectionClosedError:<br />
   &#8230;</p>
<p>found this pattern useful, as it lets the regexp hacks stay in an independantly maintained library.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: S2</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-269</link>
		<dc:creator>S2</dc:creator>
		<pubDate>Fri, 23 May 2008 17:59:06 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-269</guid>
		<description>&#62; Exceptions should not be created for each and 
&#62; every error case, that is just silly.

Why? Every error has it's exception. That's how it should be when you write nice clean code. If I throw a GenericException for every error I could possibly have, I will never be able to tell what error was really thrown and what I can do about it. On the other side, if I have an Exception for every error, I can identify every error and handle it the way it should be handled.</description>
		<content:encoded><![CDATA[<p>&gt; Exceptions should not be created for each and<br />
&gt; every error case, that is just silly.</p>
<p>Why? Every error has it&#8217;s exception. That&#8217;s how it should be when you write nice clean code. If I throw a GenericException for every error I could possibly have, I will never be able to tell what error was really thrown and what I can do about it. On the other side, if I have an Exception for every error, I can identify every error and handle it the way it should be handled.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fredrik Holmström</title>
		<link>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-267</link>
		<dc:creator>Fredrik Holmström</dc:creator>
		<pubDate>Fri, 23 May 2008 16:30:59 +0000</pubDate>
		<guid>http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/#comment-267</guid>
		<description>&gt; First of all, I hope you realise you can package an id with an exception. 
&gt; Have you also checked the sfDatabaseException class to see if they 
&gt; package the database error code with the exception, my guess is they do.
Yes I'm well aware that you can link an error code with an exception, but no neither symfony (nor any of the other libs I took as example) does this.

&gt; I see how you could make this assumption, but I am afraid you are wrong. &gt; Exceptions should not be created for each and every error case, that is just silly.
I never said that exceptions should be created for every error case, but errors that do stand out and that also make sense to refactor out into their own class should.

&gt; The correct method (as used by Symphony)
Which it isn't.
 
I wouldn't go so far as saying the correct method, but using an error code is also a good way to discriminate exceptions - thought not a method I use personally that much. 

I agree with your idea that the code should be used to discriminate different types of errors within the same class of exceptions, but I don't agree on the fact that all database exceptions should go under one gigantic "DatabaseException" as you suggest.

There is a clear advantage to be able to control the height an exception bubles to through the callstack, which is a lot harder to do with an error code.</description>
		<content:encoded><![CDATA[<p>> First of all, I hope you realise you can package an id with an exception.<br />
> Have you also checked the sfDatabaseException class to see if they<br />
> package the database error code with the exception, my guess is they do.<br />
Yes I&#8217;m well aware that you can link an error code with an exception, but no neither symfony (nor any of the other libs I took as example) does this.</p>
<p>> I see how you could make this assumption, but I am afraid you are wrong. > Exceptions should not be created for each and every error case, that is just silly.<br />
I never said that exceptions should be created for every error case, but errors that do stand out and that also make sense to refactor out into their own class should.</p>
<p>> The correct method (as used by Symphony)<br />
Which it isn&#8217;t.</p>
<p>I wouldn&#8217;t go so far as saying the correct method, but using an error code is also a good way to discriminate exceptions - thought not a method I use personally that much. </p>
<p>I agree with your idea that the code should be used to discriminate different types of errors within the same class of exceptions, but I don&#8217;t agree on the fact that all database exceptions should go under one gigantic &#8220;DatabaseException&#8221; as you suggest.</p>
<p>There is a clear advantage to be able to control the height an exception bubles to through the callstack, which is a lot harder to do with an error code.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
