Re: [RFC Discussion] Catching multiple exception types
| From: | Arvids Godjuks | Date: | Wed, 09 Mar 2016 13:27:44 +0000 |
| Subject: | Re: [RFC Discussion] Catching multiple exception types | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91587@lists.php.net to get a copy of this message | ||
I'm definetly lately started to use instanceof to check the type of
exception I've got and throw further those that I don't need. This is
especially valuable for CLI applications (I have a few daemons in PHP).
So a big +1 on the proposal idea, as for the tech part - maybe unions is a
good idea.
Arvids Godjuks.
On Wed, 9 Mar 2016, 15:24 Pierrick Charron, <pierrick@adoy.net> wrote:
> On 9 March 2016 at 08:08, Marco Pivetta <ocramius@gmail.com> wrote:
>
> > On 9 March 2016 at 14:03, Pierrick Charron <pierrick@adoy.net> wrote:
> >
> >> Hi Derick
> >>
> >> I agree that most of the time the best solution is to implement a clean
> >> exception hierarchy but as stated in the RFC :
> >>
> >> "A solution to fix this problem on the user level would be to implement
> a
> >> common interface for ExceptionType1 and ExceptionType2 and catch it.
> >> However, this is only possible when you control the exception hierarchy
> in
> >> your own code, but not possible when you don't control the code."
> >>
> >
> > I understand the use-case, but I don't see it as a widespread scenario.
> In
> > most cases, I've been doing something like following:
> >
> >
> I agree that this the RFC will not get the oscar for feature of the year,
> but I think it will lead in those few use-case to more readable code.
>
>
> > public function stuff()
> > {
> > try {
> > $this->willFail();
> > } catch (FirstException $e) {
> > $this->handleFailure($e);
> > } catch (SecondException $e) {
> > $this->handleFailure($e);
> > } catch (ThirdException $e) {
> > $this->handleFailure($e);
> > }
> > }
> >
> >
> The thing I don't like about this approach is that I have to read the code
> and double check to make sure that the catch statement call the same
> method.
> For the amount of work that needs to be done in the Engine (see the patch
> attached to the RFC) this is far more readable and it is clear that the
> code to handle those 3 exceptions is the exact same one. And if the code of
> handleFailure is small you can even put it at this single place.
>
> public function stuff()
> {
> try {
> $this->willFail();
> } catch (FirstException | SecondException | ThirdException $e) {
> $this->handleFailure($e);
> }
> }
>
> Even then, this is a rare eventuality, and as pointed out above, usually
> > fixed when wrapping exceptions correctly, if you have control over the
> > exception types).
> > Seems way below the 80/20 use-case to me, and introduces syntax changes
> as
> > well.
> >
> > Cheers,
> >
> > Marco Pivetta
> >
> > http://twitter.com/Ocramius
> >
> > http://ocramius.github.com/
> >
> >
>