Generator::throw
(PHP 5 >= 5.5.0, PHP 7)
Generator::throw — Throw an exception into the generator
Описание
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.
Список параметров
-
exception
-
Exception to throw into the generator.
Возвращаемые значения
Returns the yielded value.
Примеры
Пример #1 Throwing an exception into a generator
<?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
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник языка
- Встроенные интерфейсы и классы
- Функция Generator::current() - Get the yielded value
- Generator::getReturn
- Функция Generator::key() - Get the yielded key
- Функция Generator::next() - Resume execution of the generator
- Функция Generator::rewind() - Rewind the iterator
- Функция Generator::send() - Send a value to the generator
- Функция Generator::throw() - Throw an exception into the generator
- Функция Generator::valid() - Check if the iterator has been closed
- Функция Generator::__wakeup() - Serialize callback
Коментарии
$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'));