NYCPHP Meetup

NYPHP.org

[nycphp-talk] If/else vs Try/catch

Kenneth Downs ken at secdat.com
Wed Nov 28 12:53:21 EST 2007


Picture this example also:

function try_include($filename) {
    try {
       include($filename);
    }
    catch Exception(e) {
       echo "Problem trying to include file!";
       return false;
    }
    return true;
}

Ben Sgro (ProjectSkyLine) wrote:
> Hello Chris,
> Good points again. I've been doing some reading
> to get a better grasp on it.
>
> http://www.w3schools.com/php/php_exception.asp
>
> Seems to be a good explanation.
>
> Thanks.
>
> - Ben
>
> ----- Original Message ----- From: "csnyder" <chsnyder at gmail.com>
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Sent: Wednesday, November 28, 2007 11:38 AM
> Subject: Re: [nycphp-talk] If/else vs Try/catch
>
>
>> On Nov 28, 2007 11:25 AM, Ben Sgro (ProjectSkyLine)
>> <ben at projectskyline.com> wrote:
>>> Thanks. I'll read up on it now...and post my thoughts.
>>
>> Trying to explain the benefits of try/catch is like trying to explain
>> the benefits of OO code: you don't need it to get the job done, but it
>> really helps if you want to get the job done elegantly.
>>
>> If you find yourself writing code like:
>>
>> $success = $obj->process1();
>> if ( $success ) {
>>  $success = $obj->process2();
>>  if ( $success ) {
>>    $success = $obj->process3();
>>  }
>> }
>> if ( !$success ) {
>>  exit( "An error ocurred in either process 1, 2, or 3." );
>> }
>>
>> ... then try/catch is the way out of your nightmare.
>>
>> try {
>>  $obj->process1();
>>  $obj->process2();
>>  $obj->process3();
>> } catch Exception( e ) {
>>  exit( "An error occurred: ".$e->message() );
>> }
>>
>> Error handling doesn't need to be part of your program logic anymore.
>>
>> -- 
>> Chris Snyder
>> http://chxo.com/
>> _______________________________________________
>> New York PHP Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>>
>> NYPHPCon 2006 Presentations Online
>> http://www.nyphpcon.com
>>
>> Show Your Participation in New York PHP
>> http://www.nyphp.org/show_participation.php
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php


-- 
Kenneth Downs
Secure Data Software, Inc.
www.secdat.com    www.andromeda-project.org
631-689-7200   Fax: 631-689-0527
cell: 631-379-0010




More information about the talk mailing list