(PHP 5 >= 5.5.0, PHP 7)
Throw an exception into the generator
public mixed Generator::throw ( Exception $exception )
Throws an exception into the generator and resumes execution of the generator. The behavior will be the same as if the current yield expression was replaced with a throw $exception statement.
If the generator is already closed when this method is invoked, the exception will be thrown in the caller's context instead.
Parameters:
exception
Exception to throw into the generator.
Returns:
Returns the yielded value.
Examples:
Throwing an exception into a generator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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' )); ?> |
The above example will output:
Foo Exception: Test Bar
Please login to continue.