[nycphp-talk] If/else vs Try/catch
Ben Sgro (ProjectSkyLine)
ben at projectskyline.com
Wed Nov 28 12:44:35 EST 2007
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
More information about the talk
mailing list