Re: [RFC Discussion] Catching multiple exception types
| From: | Niklas Keller | Date: | Wed, 09 Mar 2016 12:04:50 +0000 |
| Subject: | Re: [RFC Discussion] Catching multiple exception types | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91583@lists.php.net to get a copy of this message | ||
2016-03-09 12:52 GMT+01:00 Derick Rethans <derick@php.net>:
> Hi!
>
> On Tue, 8 Mar 2016, Pierrick Charron wrote:
>
> > Bronisław Białek and I would like to start a discussion about allowing
> > multiple exception types to be caught in a single catch statement.
> >
> > https://wiki.php.net/rfc/multiple-catch
>
> Would it not be better, to have your Exceptions extend a common base
> class in this case? Or perhaps, which I think is even better, to have
> your Exception classes implement a common interface that you can catch
> against. That I believe should already work.
>
This assumes they're my exceptions and I can change the code. Doesn't work
with libraries having not that good exception structures or even leaking
their dependency's exceptions.
> What you are asking PHP users to do with a multiple catch like this, is
> to test in each catch's statement block to test for each class again.
>
No, usually it's enough that they're all extending Exception / Throwable.
You just can't catch Exception / Throwable, because that would catch all
exceptions. If you have a multi-catch, it doesn't matter which exception is
thrown and it's never checked, otherwise you'd use two blocks directly.
Regards, Niklas
> Neither the inheritence or implements options from the previous
> paragraph have that issue.
>
> cheers,
> Derick
>