php-src icon indicating copy to clipboard operation
php-src copied to clipboard

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

Open bwoebi opened this issue 4 years ago • 3 comments

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

bwoebi avatar Mar 29 '22 11:03 bwoebi

Confirmed.

cmb69 avatar Mar 29 '22 12:03 cmb69

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

alex-kalanis avatar Mar 30 '22 09:03 alex-kalanis