Skip to content

Exceptions thrown within a yielded from iterator are not rethrown into the generator #8289

Closed

Description

@bwoebi

Description

The following code (https://3v4l.org/pvIjH):

<?php

function yieldFromIteratorGeneratorThrows() {
    try {
        yield from new class(new ArrayIterator([1, -2])) extends IteratorIterator {
            public function key() {
                if ($k = parent::key()) {
                    throw new Exception;
                }
                return $k;
            }
        };
    } catch (Exception $e) {
        yield 2;
    }
}

foreach (yieldFromIteratorGeneratorThrows() as $k => $v) {
    var_dump($v);
}

Resulted in this output:

int(1)

But I expected this output instead:

int(1)
int(2)

PHP Version

PHP 7.2 - master

Operating System

No response

Activity

  1. self-assigned this
    on Apr 1, 2022
  2. added a commit that references this issue on Apr 1, 2022
    1364945
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions