php-src/Zend/tests/generators/throw_rethrow.phpt
Nikita Popov 02dca18b90 Fix bug #65764
I'm not exactly sure whether this is the right way to fix it. The
question is whether Generator::throw() on a newborn generator (i.e.
a generator that is not yet at yield expression) should first advance to
the first yield and throw the exception there or whether it should
instead throw the exception in the caller's context.

The old behavior was to throw it at the start of the function (i.e.
the very first opcode), which causes issues like the one in #65764.
Effectively it's impossible to properly handle the exceptions in this
case.

For now I choose the variant where the generator advances to the
first yield before throwing, as that's consistent with how all other
methods on the Generator object currently behave. This does not
necessarily match the behavior in other languages, e.g. Python would throw
the exception in the caller's context. But then our send() method already
has this kind of deviation, so it stays internally consistent at least.
2013-12-01 13:37:56 +01:00

35 lines
698 B
PHP

--TEST--
Generator::throw() where the generator throws a different exception
--FILE--
<?php
function gen() {
echo "before yield\n";
try {
yield;
} catch (RuntimeException $e) {
echo 'Caught: ', $e, "\n\n";
throw new LogicException('new throw');
}
}
$gen = gen();
var_dump($gen->throw(new RuntimeException('throw')));
?>
--EXPECTF--
before yield
Caught: exception 'RuntimeException' with message 'throw' in %s:%d
Stack trace:
#0 {main}
Fatal error: Uncaught exception 'LogicException' with message 'new throw' in %s:%d
Stack trace:
#0 [internal function]: gen()
#1 %s(%d): Generator->throw(Object(RuntimeException))
#2 {main}
thrown in %s on line %d