$gen = (function () {
try {
yield 1;
} catch (Exception $e) {
echo $e->getMessage();
}
})();
$gen->throw(new Exception('gen throw exception'));
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
Generator::throw — 向生成器抛出异常
exception
要抛出到生成器的异常。
返回生成的返回值。
示例 #1 向生成器抛出异常
<?php
function gen() {
echo "Foo\n";
try {
yield;
} catch (Exception $e) {
echo "Exception: "{$e->getMessage()}\n";
}
echo "Bar\n";
}
$gen = gen();
$gen->rewind();
$gen->throw(new Exception('Test'));
?>
以上示例将输出
Foo Exception: Test Bar
$gen = (function () {
try {
yield 1;
} catch (Exception $e) {
echo $e->getMessage();
}
})();
$gen->throw(new Exception('gen throw exception'));
$gen = (function () {
try {
yield 1;
} catch (Exception $e) {
echo $e->getMessage();
}
})();
$gen->throw(new Exception('gen throw exception'));