Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:1443 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92522 invoked from network); 11 May 2003 16:52:43 -0000 Received: from unknown (HELO mailout10.sul.t-online.com) (194.25.134.21) by pb1.pair.com with SMTP; 11 May 2003 16:52:43 -0000 Received: from fwd01.sul.t-online.de by mailout10.sul.t-online.com with smtp id 19Eu40-0005um-08; Sun, 11 May 2003 18:52:40 +0200 Received: from baumbart.post.rwth-aachen.de (520072483730-0001@[80.142.175.63]) by fwd01.sul.t-online.com with esmtp id 19Eu3t-0XSSxMC; Sun, 11 May 2003 18:52:33 +0200 Reply-to:marcus.boerger@post.rwth-aachen.de Message-ID: <5.1.0.14.2.20030511185049.015c3d90@pop.t-online.de> X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Sun, 11 May 2003 18:52:31 +0200 To: Theo Spears Cc: internals@lists.php.net In-Reply-To: <200305111640.51779.postmaster@terrarium.f9.co.uk> References: <1052077704.11371.319.camel@hasele> <5.1.0.14.2.20030504021938.09452980@mailbox.rwth-aachen.de> <1052077704.11371.319.camel@hasele> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Sender: 520072483730-0001@t-dialin.net Subject: Re: [PHP-DEV] exceptions instead of errors From: marcus.boerger@t-online.de ((Marcus =?iso-8859-1?Q?B=F6rger?=)) At 17:40 11.05.2003, Theo Spears wrote: >On Sunday 04 May 2003 20:48, Sterling Hughes wrote: > > Just a note that I'd really like to see this. One of the great things > > about exceptions will be: > > > > try { > > $m = mysql_connect("localhost", "user", "pass"); > > $sth = mysql_query("select bar from foo", $m); > > while ($row = mysql_fetch_array($sth)) { > > echo $row[0]; > > } > > mysql_close($m); > > } catch (exception $e) { > > echo $e->getMessage(); > > } > > > > This allows you to remove all the nasty if ($conn) could, and move your > > error handling into a singular place. I *really* would like this for > > internal functions. We could also make a new type of exception > > "internalexception," which a user has to specifically catch. This way > > unless you explictly specify that you want to catch an > > internalexception, it will default to just outputting an error message. > > >Although this approach would certainly simplify handling errors it seems >to me >it would give everyone big problems when it comes to predicting the control >flow of their programs. For example > >function db_connect ( $db_name ) { > $link = mysql_connect(); > if ( ! mysql_select_db( $db_name, $link ) ) { > mysql_select_db ( BACKUP_DB_NAME, $link ); > } >} > >now, depending on whether you do > >try { > db_connect ( 'foo' ); >} catch (exception e) { >} > >or just > >db_connect ( 'foo' ); > >The function will behave differently. Even if all new PHP5 code uses >exceptions there is a lot of code which doesn't, and so the principles of >information hiding say you would have to make sure any code which calls a >function in a separate module has to make sure that function isn't inside a >try{}catch{} block, and the only functions in the same module which are >inside try{}catch{} blocks that catch internal exceptions don't call any >functions in external modules. > >Personally I would prefer a system similar to the @ notation which suppresses >error messages, possibly make %function() or similar cause the function to >raise an exception. (The engine could handle this by silently dropping all >exceptions from internal functions that weren't called with the relevant >prefix). The rule "try/catch = exception" would be easy enough to understand. Changing all code to use exceptions would also work since the bahaviour would fall back to the normal one if you missed to define a try/catch block. marcus