[nycphp-talk] If/else vs Try/catch
Ben Sgro (ProjectSkyLine)
ben at projectskyline.com
Wed Nov 28 13:33:42 EST 2007
Hello Brian,
Great points. I've been playing with some code examples to get a feel for
it.
One thing I've stumbled upon that might be nice is the following:
Which allows to me handle Exceptions two ways, one for internal logging
and one for display to the user. I'll read the link you posted as well.
Thanks,
- Ben
class LogException extends Exception
{
public function errorMessage( )
{
file_put_contents('exception_log.txt', "\n" . $this->getMessage( ) .
"\n");
}
}
class UserException extends Exception
{
public function errorMessage( )
{
return "An error has occured with " . $this->getMessage( ) . "\n";
}
}
$email = "ben at example.com";
try
{
try
{
if (strpos($email, "example") !== FALSE)
{
throw new LogException($email);
}
}
catch(LogException $e)
{
$e->errorMessage( );
/* throw exception again. */
throw new UserException($email);
}
}
catch (UserException $e)
{
echo $e->errorMessage( );
}
----- Original Message -----
From: "Brian D." <brian at realm3.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
Sent: Wednesday, November 28, 2007 1:22 PM
Subject: Re: [nycphp-talk] If/else vs Try/catch
> Ben,
>
> Chris Snyder gave a good example of why you want to use error
> handling, but one other thing you may want to look at is the built-in
> PHP Exception class. This article had some code samples:
> http://www.phpbuilder.com/manual/en/language.exceptions.php
>
> The example you gave with try_include() doesn't tell the user/developer:
> - why it failed
> - where it failed
> - how important is the failure (is it fatal?)
>
> All you know is that you got false back, now you have to figure out if
> that's going to break something or not. Exceptions allow you to pass
> back different types of failures, details about the failure, the level
> of severity, and much more that you wouldn't get back from a
> true/false return.
>
> One of the most important things to consider, in my opinion, is that
> exceptions are a *standardized* way to handle errors. This means that
> when Joe returns a string that says "FAILURE" but Fred returned
> "false", you don't always know what to expect. If an exception is
> thrown, then you know for sure that something has failed and you need
> to handle it.
>
> - Brian
>
>
>
>
> On Nov 28, 2007 1:04 PM, Ben Sgro (ProjectSkyLine)
> <ben at projectskyline.com> wrote:
>> Hello Kenneth,
>>
>> I saw an example similiar to this on PHP.net. I don't really
>> see why try/catch is better ... Not trying to start a war here. -= ]
>>
>> function try_include( $filename )
>> {
>> if( include($filename) != 1 )
>> {
>> echo "Problem trying to include file!";
>> return false;
>> }
>> return true;
>> }
>> ----- Original Message -----
>> From: "Kenneth Downs" <ken at secdat.com>
>> To: "NYPHP Talk" <talk at lists.nyphp.org>
>>
>> Sent: Wednesday, November 28, 2007 12:53 PM
>> Subject: Re: [nycphp-talk] If/else vs Try/catch
>>
>>
>> > 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
>> >
>> > _______________________________________________
>> > 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
>>
>
>
>
> --
> realm3 web applications [realm3.com]
> freelance consulting, application development
> (423) 506-0349
> _______________________________________________
> 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