array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), 'this' => array ( 0 => 'generator.throw.php', 1 => 'Generator::throw', ), 'up' => array ( 0 => 'class.generator.php', 1 => 'Generator', ), 'prev' => array ( 0 => 'generator.send.php', 1 => 'Generator::send', ), 'next' => array ( 0 => 'generator.valid.php', 1 => 'Generator::valid', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'ja', 'path' => 'language/predefined/generator/throw.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
Generator::throw — 例外をジェネレータにスローする
例外をジェネレータにスローして、ジェネレータを続行します。
この振る舞いは、現在の yield 式の部分を
throw $exception
文に置き換えたのと同じになります。
このメソッドの起動時に既にジェネレータが閉じられている場合は、呼び出し側のコンテキストに例外がスローされます。
exception
ジェネレータにスローする例外。
yield した値を返します。
例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