Re: PHP5 - Exceptions and the future of PEAR_Error

From: Date: Mon, 26 Apr 2004 17:15:14 +0000
Subject: Re: PHP5 - Exceptions and the future of PEAR_Error
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-12134@lists.php.net to get a copy of this message
>Having been brought up in PHP exclusively, I'm rather naïve to the whole >idea of exceptions. Could someone explain how throwing exceptions would >rid me of if(!DB::isError($result))? Since I started my "programming career" with Java, I think Exceptions is a great concept of error-handling. Imagine the following: In your script is a section that plays around with db-connections and queries that are all realized over PEAR-Packages. Since you are the kind of programmer that likes his code to return every single error that occurs (hey, you are a developer, you should know why your stuff doesn't work ;) ), you implement it in PHP4 as follows: (Watch it, the following code is somewhat simplified!) <?php $oObject1 = new PEAR_DBClass; $oObject1->connect("database"); if (PEAR::isError($oObject1)) { echo "Error occured while connecting to the database :".$oObject1->getMessage(); die(); } $resultset = $oObject1->query("SELECT FOO AND SO ON YOU HAVE SOME SQL ERRORS HERE"); if (PEAR::isError($resultset)) { echo "Error occured while querying the db:".$resultset->getMessage(); die(); } . . . ?> I could go on for hours in the same way. Pretty much typing, isn't it? Now comes the concept of Exceptions, the holy grale of error-handling. Take a look at this. <?php $oObject1 = new PEAR_DBClass; try { $oObject1->connect("database"); $resultset = $oObject1->query("SELECT FOO AND SO ON YOU HAVE SOME SQL ERRORS HERE"); } catch (PEAR_Exception $exception) { echo "Damn. You got an error here. Here is the message: ".$exception->getMessage(); } ?> Pretty, huh? Makes the code shorter and more readable IMHO. Any questions? Just ask! Want to add some correction? Just do it. Regards Thorsten

« previous php.pear.general (#12134) next »