Skip to content

Generator::throw does not enter a catch inside the generator #329

@nahime0

Description

@nahime0

Description

Generator::throw() behaves correctly when the exception is caught outside the generator, but if the generator body has its own try/catch around a yield, Elephc aborts with Fatal error: uncaught exception instead of resuming the generator and entering the local catch block.

Steps to reproduce

<?php
function g() {
    try {
        yield 1;
    } catch (Exception $e) {
        echo "in,", $e->getMessage();
    }
}

$g = g();
$g->current();
$g->throw(new Exception("boom"));
echo ",done";

Expected behavior

PHP 8.4.19 resumes into the generator's catch block and prints:

in,boom,done

Actual behavior

Elephc terminates with:

Fatal error: uncaught exception

Additional context

A control case without the in-generator catch works as expected in Elephc:

<?php
function g() { yield 1; }
$g = g();
$g->current();
try {
    $g->throw(new Exception("boom"));
} catch (Exception $e) {
    echo $e->getMessage();
}

This suggests the bug is specifically in routing the thrown exception back into generator-internal exception handling.

Environment

  • Elephc commit: 02ffd63c0efdd8b5dd4069e0b6bd58dfe2a9325d
  • PHP: 8.4.19
  • OS: Darwin 25.5.0 arm64
  • Reproduced on: 2026-06-04T07:05:57Z

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions