You are here: Re: Error Trapping Within Classes « PHP Programming Language « IT news, forums, messages
Re: Error Trapping Within Classes

Posted by mootmail-googlegroups on 09/19/06 19:01

ZeldorBlat wrote:
> 47computers@gmail.com wrote:
> > > Look at trigger_error()
> > > <http://www.php.net/trigger_error>
> > >
> > > But you may really want to be using exceptions for the case described
> > > above.
> >
> > This has definitely sent me in the right direction, thank you :)
>
> > Hopefully one last question: Suppose I enclose my function's code
> > within a try/catch. When I want to manually error out, I throw a new
> > Exception with the error message I want. From a design perspective,
> > what _should_ I be doing in that catch block?
> >
> > Currently, for my testing, I'm just calling trigger_error in the catch
> > block to execute my custom error handler which is working like a charm.
> > Is this bad form in any way? This may be a silly question, but I
> > figure it's worth asking.
> >
> >
> > -David
>
> The beauty of exceptions is that if you want to handle them you can, or
> you can let someone else handle them, or you can not handle them at
> all. Furthermore, you can decide what to do with it based on some sort
> of condition. So, for example you might say:
>
> try {
> //do something that might throw an exception with code = 1
> //do something that might throw an exception with code = 2
> } catch (Exception $e) {
> if($e->getCode() == 1) {
> //I will gracefully handle the exception when the code is 1
> }
> else {
> throw $e; //let someone else deal with it
> }
> }

Extending upon that example, Exceptions can also give you a greater
granularity as to the TYPE of errors that occur. By inheriting the
base Exception class into your own customized versions, you can have
multiple types of exceptions that are handled differently. So not only
can you decide whether to handle an exception or throw it up the stack,
but you can also handle each type in it's own way. For example, adding
to the quoted example above:

try {
//do something that might throw an exception with code = 1
//do something that might throw an exception with code = 2
} catch (DatabaseException $e) {
//my query returned no results, so I want to handle that right here
} catch (Exception $e) {
//a generic exception occured
if($e->getCode() == 1) {
//I will gracefully handle the exception when the code is 1
}
else {
throw $e; //let someone else deal with it
}
}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация