Generator
PHP Manual

Generator::throw

(PHP 5 >= 5.5.0)

Generator::throwLance une exception dans le générateur

Description

public mixed Generator::throw ( Exception $exception )

Lance une exception dans le générateur et reprend son exécution. Le comportement sera le même que si l'expression yield courante ait été remplacée avec une expression throw $exception.

If the generator is already closed when this method is invoked, the exception will be thrown in the caller's context instead.

Liste de paramètres

exception

Exception à lancer dans le générateur.

Exemples

Exemple #1 >Lance une exception dans un générateur

<?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'));
?>

L'exemple ci-dessus va afficher :

Foo
Exception : Test
Bar

Valeurs de retour

Retourne la valeur cédée.


Generator
PHP Manual