<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fredrikholmstrom.com &#187; PHP</title>
	<atom:link href="http://fredrikholmstrom.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://fredrikholmstrom.com</link>
	<description>Java is to javascript what car is to carpet</description>
	<lastBuildDate>Sun, 16 Aug 2009 09:19:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exceptions, you&#8217;re doing it wrong</title>
		<link>http://fredrikholmstrom.com/exceptions-youre-doing-it-wrong/</link>
		<comments>http://fredrikholmstrom.com/exceptions-youre-doing-it-wrong/#comments</comments>
		<pubDate>Fri, 23 May 2008 14:29:37 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=10</guid>
		<description><![CDATA[Once again a post aimed at the PHP community, not so much of a rant but more of something I&#8217;ve seen done horribly wrong in a lot of PHP code recently, first let me take a few examples from a couple of well known PHP frameworks and libraries:
symfony, file: lib/database/sfMySQLDatabase.class.php

$error = 'Failed to create a [...]]]></description>
			<content:encoded><![CDATA[<p>Once again a post aimed at the PHP community, not so much of a rant but more of something I&#8217;ve seen done horribly wrong in a lot of PHP code recently, first let me take a few examples from a couple of well known PHP frameworks and libraries:</p>
<p><strong>symfony</strong>, <em>file: lib/database/sfMySQLDatabase.class.php</em></p>
<pre class="prettyprint">
$error = 'Failed to create a MySQLDatabase connection';
throw new sfDatabaseException($error);
</pre>
<p><strong>propel</strong>, <em>file: runtime/classes/propel/util/BasePeer.php</em></p>
<pre class="prettyprint">
throw new PropelException("Expecting to delete 1 record, but criteria match multiple.");
</pre>
<p><strong>doctrine</strong>, <em>file: lib/Doctrine/Connection.php</em></p>
<pre class="prettyprint">
throw new Doctrine_Connection_Exception('First argument should be an instance of
PDO or implement Doctrine_Adapter_Interface');
</pre>
<p>I could go on and on and list a couple of hundred of these from each of most poplar PHP libraries, and they all make the same assumption: An exception is a fatal error. And by making this assumption and using one monolithic &#8220;DatabaseException&#8221;-class, it becomes impossible to handle the exceptions in any other manner then as a fatal errors.</p>
<p>For example the first snippet, coming from the symfony framework is thrown when you can&#8217;t connect to a database. Since every other error that can happen to any database connection done in symfony also throws a &#8220;sfDatabaseException&#8221; how is the user of the library supposed try a backup database or supply a custom &#8220;database is down&#8221;-error page ? By regex:ing the message of the thrown exception?</p>
<p>Exceptions are not, and I repeat <strong>not</strong>, a fatal error mechanism (they can be, sure &#8211; but it&#8217;s not their only or primary use). Taken the above symfony code again, it should look something like this:</p>
<pre class="prettyprint">
throw new DatabaseConnectionFailedException('MySQL');
</pre>
<p>Or something along those lines, making it possible to somehow distinguish between different type of exceptions allows us to do something like this (again assuming the symfony code):</p>
<pre class="prettyprint">
try {
         // Try to connect to a database

} catch (DatabaseConnectionFailedException $e)  {
         // Try backup database

} catch (DatabaseException $e) {
         // Generic database error
}
</pre>
<p>So, to sum it up: </p>
<h1><strong>Exceptions, you&#8217;re doing it wrong!</strong></h1>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/exceptions-youre-doing-it-wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
