Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions thrown during yield operation fall through the generator instead of being handled within #8269

Open
bwoebi opened this issue Mar 29, 2022 · 3 comments

Comments

@bwoebi
Copy link
Member

bwoebi commented Mar 29, 2022

Description

The following code:

<?php

set_error_handler(function() { throw new \Error(); });

function &gen() {
    try {
        yield 1;
    } catch (Error $e) {
        print "Caught";
    }
}

foreach (gen() as $y);

Resulted in Uncaught Error

But I expected this output instead:

Caught

return_by_ref has the same problem, it seems to me, however in generators there's the additional case of exceptions in destructors of previous key and values.

I wouldn't change the longstanding behaviour in current PHP versions, but should be fixed for PHP 8.2 possibly, for academical correctness at least.

PHP Version

master

Operating System

No response

@cmb69
Copy link
Member

cmb69 commented Mar 29, 2022

Confirmed.

@damianwadley
Copy link
Member

Note that return statements behave the same.

@alex-kalanis
Copy link

alex-kalanis commented Mar 30, 2022

Similar problem but another point - call non-exitent trait in autoloading throws an un-catchable exception - even when it's expected.

My testing repo: https://github.com/alex-kalanis/autoload_testing
Travis check of original repo: https://github.com/alex-kalanis/kw_autoload/runs/5751725521

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants