Generator::throw

(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.

Список параметров

exception

Exception to throw into the generator.

Примеры

Пример #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

Возвращаемые значения

Returns the yielded value.

Коментарии

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";
}

 gen();

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

$myarray=array();

if($respuesta == "Mark"){

for($i=5;$i<=16;$i++){
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>";
}//if

}//catch
   

}//for

}//if
2016-08-30 12:31:24
http://php5.kiev.ua/manual/ru/generator.throw.html
$gen = (function () {
    try {
        yield 1;
    } catch (Exception $e) {
        echo $e->getMessage();
    }
})();

$gen->throw(new Exception('gen throw exception'));
2017-11-13 13:37:39
http://php5.kiev.ua/manual/ru/generator.throw.html
$gen = (function () {
    try {
        yield 1;
    } catch (Exception $e) {
        echo $e->getMessage();
    }
})();

$gen->throw(new Exception('gen throw exception'));
2017-11-13 13:38:23
http://php5.kiev.ua/manual/ru/generator.throw.html

    Поддержать сайт на родительском проекте КГБ