$gen = (function () {
try {
yield 1;
} catch (Exception $e) {
echo $e->getMessage();
}
})();
$gen->throw(new Exception('gen throw exception'));
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
Generator::throw — 向生成器抛出异常
exception
要抛入生成器的异常。
返回产生的值。
示例 #1 向生成器抛出异常
<?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
$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'));
有错误,正确的方式是
function gen() {
echo "Foo\n";
try {
throw new Exception('Prueba');
} catch (Exception $e) {
echo "Excepción: {$e->getMessage()}\n";
}
echo "Bar\n";
}
gen();
我找到了如何使用 getMessage 的技巧,看看吧
$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