Re: Best practice concerning Exceptions
| From: | Chuck Burgess | Date: | Thu, 11 Mar 2010 15:16:41 +0000 |
| Subject: | Re: Best practice concerning Exceptions | ||
| References: | 1 2 3 4 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-30397@lists.php.net to get a copy of this message | ||
On Thu, Mar 11, 2010 at 9:05 AM, Auke van Slooten <auke@muze.nl> wrote:
> Michael Gauthier wrote:
>
>> On Thu, 2010-03-11 at 10:51 +1030, Daniel O'Connor wrote:
>>
>>> On Thu, Mar 11, 2010 at 1:07 AM, Auke van Slooten <auke@muze.nl> wrote:
>>>
>>> I find that most people (myself included) just rely on a general category
>>> of
>>> exception; and rarely if ever make use of exception codes.
>>>
>>> [snip]
>
> Hi Auke,
>>
>> I typically create a new exception class for each category of error and
>> then use codes to distinguish specific errors in those categories. I
>> like to use and extend the built-in SPL exceptions where applicable.
>>
>> For example, in a web service API wrapper, I might have:
>>
>> * Foo_Exception
>> * Foo_HttpException
>> * Foo_AuthenticationException
>>
>> Then I might have more specific exceptions for specific circumstances.
>>
>> When deciding whether or not to create a new exception class, I think
>> about how it would be to write error handling code for the error in
>> question, and think about how the error should be logged.
>>
>
> Hi,
>
> thanks for answering. I've changed the API to incorporate more Exception
> classes. Still just four though, I couldn't think of any usefull case for
> more...
>
> I like the idea of using common SPL exceptions, but unless I'm mistaken you
> lose the ability to catch any library with one catch() statement? Currently
> all my exceptions inherit from the main exception class, so you can just
> 'catch( Ripcord_Exception $e )' and any exception the library throws will be
> caught. Using SPL exceptions, you would either also have to catch the SPL
> base exception or simply catch all exceptions.
>
> BTW, I've kept the unique error codes, since I think it's easier to use if
> you just want to have a single error handler and it adds no extra overhead.
>
>
> regards,
> Auke van Slooten
> Muze
>
Hi Auke,
To help with your concern, have a look at how we're organizing exceptions in
PEAR2 --
http://wiki.php.net/pear/rfc/pear2_exception_policy#full_code_example
In short, you use a base exception *interface* rather than *class* for your
package's "core" exception. Your concrete exception classes can then extend
SPL exceptions while implementing your package's exception interface, thus
making *both* characteristics catchable.
Hope that helps...
--
CRB