Re: Re: Are exceptions allowed in php core?
| From: | Derick Rethans | Date: | Mon, 03 Sep 2012 11:16:34 +0000 |
| Subject: | Re: Re: Are exceptions allowed in php core? | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62722@lists.php.net to get a copy of this message | ||
On Sun, 2 Sep 2012, Gustavo Lopes wrote:
> On Sun, 02 Sep 2012 02:27:02 +0200, Rasmus Lerdorf <rasmus@lerdorf.com> wrote:
>
> > $things = getStuff();
> > foreach($things as $thing) {
> > do_some($thing);
> > }
> >
> > which is very normal PHP that you see in millions of lines of code, I
> > would not expect to have to wrap my foreach in a try/catch here.
> > [...]
> >
> > this is still going to fatal on me eventually with an uncaught exception
> > on the foreach if, for example, I pass the generator a second time to
> > something and it has run off the end. This is something you don't have
> > to worry about currently and I think it will cause headaches for people
> > when they start passing generators around. In theory I should be able to
> > pass a generator to any existing code that takes an array without having
> > to worry about modifying that code to catch foreach exceptions. And yes,
> > I know it is actually a generator exception, but to the average user it
> > will look like it is foreach throwing.
>
> I think the first thing anyone who uses generators must understand is that
> they are iterators.
You forget that not even everybody that uses PHP knows what an iterator
is. For all they care, they have an array they can run foreach() on.
> They should be compared with iterators, not arrays.
They shouldn't be compared with either. They are magical things that
happen with a "yield" keyword.
> With that in mind, the behavior is not surprising for anyone who knows
> how iterators and foreach interact.
>
> More importantly, there is no other satisfactory solution (except a fatal
> error). foreach has no return value, so it has no other way to signal a
> failure. If we used a notice or a warning here what would happen is that code
> that used generators with an invalid state would, except for the notice, work
> as if it had been given an empty iterator. Put another way, it would fail with
> only a notice, and continue.
Continue doing what?
cheers,
Derick