RE: [PEAR-DEV] Exception misinformation
| From: | David Ashwood | Date: | Mon, 12 Jul 2004 08:22:41 +0000 |
| Subject: | RE: [PEAR-DEV] Exception misinformation | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-31897@lists.php.net to get a copy of this message | ||
I haven't been playing with PHP5's exception handler yet but If you could
override the base system Exception class you could do something like the
following - which would be similar to the way Pear manages errors (treat the
following as pseudo code):
Class MyErrorHandler extends System.Exception {
Var $ExpectedExceptions=array();
Function __Throw($e) {
// If the exception isn't in $ExpectedExceptions - if it is - then nothing
is thrown - and // the code continues
Throw $e;
}
Function _ExpectExceptions($ExceptionToExpect) {
}
}
$exceptionHandler= New System.Exception();
$MyErrorHandler = & New MyErrorHanlder();
$exceptionHandler::CustomHandler = &$MyErrorHandler;
$MyErrorHanlder::_ExpectExceptions(new exception("something"));
...
$MyErrorHanlder::_ExpectExceptions(new exception("something else"));
Then later that same day :)..
Try {
// Do some code that throws the exception
// The exception handler determines if there is a custom handler
// There is - so it uses that handler (MyErrorHandler) - which we have
defined
} catch (Exception $e) {
// This is something we didn't expect
Switch ($e):
Case Exception_A:
Break;
Case Exception_B:
Break;
Default:
Throw $e;
Break;
}
With the above you get the same kinda flexibility as Pear::Exception - but
in a PHP5 way. PEAR::Exceptions would be a subclass of the
System.Exceptions - so it wouldnt matter if a PEAR::Exception was thrown or
a System::Exception. That way you don't have to mandate what type of
exception was thrown and the stack trace would still be meaningful.
I guess I should take a look at PHP5 Exceptions and see if the above is
possible.
Anybody had much of a play with them yet?
Regards,
David Ashwood
-----Original Message-----
From: Sergio Carvalho [mailto:sergio.carvalho@portugalmail.com]
Sent: Sunday, July 11, 2004 9:53 PM
To: pear-dev@lists.php.net
Subject: Re: [PEAR-DEV] Exception misinformation
Mandate that PEAR Exceptions get thrown by a PEAR method. Instead of:
throw new SomeFangledException('aha');
Developers should write:
PEAR_Exception::throw(new SomeFangledException('aha'));
You can then do all kinds of magic, from silencing to downgrading into
Warnings. This has at least one defect: the stack trace for the
exception will start inside PEAR_Exception::throw. The first meaningful
location will the the next level in the call stack.
Complicating a bit the throwing code, one could mandate PEAR_Exceptions
are thrown like this:
if (PEAR_Exception::shouldThrow($e = new SomeFangledException('aha')))
throw(e);
Cheers,
Sérgio Carvalho
Lukas Smith wrote:
> Hi,
>
> I am wondering if we have come up with some magic to handle the
> following issue with exceptions. Currently using PEAR_Error I can build
> up a stack of expected errors which should not cause the standard error
> handling callback to be called and thereby let local error handling take
> over.
>
> The only way using Exceptions I can quickly come up with is wrapping
> every method call with a try catch block including _every_ method call.
> Considering that this catch block could get fairly lengthy this could
> bloat code considerably.
>
> Again this shows a huge problem in how exceptions force handling of
> every error and at the same time not being able to be easily selectively
> suppresed from showing this behaviour.
>
> Anyways not sure that I thought this thing 100% through considering its
> a sunday evening and I am currently watching a movie on TV. If I am
> totaly off .. sorry for the noise.
>
> regards,
> Lukas