PHP 7.2.0 Beta 3 Released


(PHP 5 >= 5.5.0)

Generator::throwThrow 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.

Elenco dei parametri


Exception to throw into the generator.


Example #1 Throwing an exception into a generator

function gen() {
    try {
    } catch (
Exception $e) {
"Exception: {$e->getMessage()}\n";
$gen gen();
$gen->throw(new Exception('Test'));

Il precedente esempio visualizzerà:

Exception: Test

Valori restituiti

Returns the yielded value.

add a note add a note

User Contributed Notes 1 note

jeudipolanco at ymail dot com
11 months ago
there have errors the form correct to do that is of this way:

function gen() {
    echo "Foo\n";
    try {
    throw new Exception('Prueba');

    } catch (Exception $e) {
        echo "Excepción: {$e->getMessage()}\n";
    echo "Bar\n";


i resolved one tecnic of how work getMessage look at it:


if($respuesta == "Mark"){

try {

    $palabra="soy la excepcion en la linea:".$i;

     throw new Exception($palabra);

catch(Exception $e) {

        $myarray[$e->getLine()] =$palabra;
        if($e->getLine() == $i){
  echo "<br><b>La excepción se creó en la línea : " .$e->getLine()."LA LINEA DICE:". $myarray[$e->getLine()]."</b>";



To Top