我有一个这样的循环
foreach ($array as $row) {
$row->executeThatFunction();
}
.
.
.
public function executeThatFunction($someVariable) {
// do something that may craches
}
所以我想继续循环,即使executeThatFunction()崩溃(php错误为例)
我该怎么做呢?PS:我正在symfy2项目中工作,所以如果symfony提供了一些解决方案,我将很高兴学习它。
Thanks in advance
EDIT:
我可以这样做吗?
foreach ($array as $row) {
try {
$row->executeThatFunction();
} catch($e)
{
continue;
}
}
如果您使用php7: php7抛出异常等错误。所有可恢复的错误都是可捕获的。而且错误和异常都实现了一个称为Throwable
的公共接口。
这意味着当发生可抛出错误时,可以用try-catch块包围调用并继续循环:
foreach ($array as $row) {
try {
$row->executeThatFunction();
} catch (Throwable $t) {
// you may want to add some logging here...
continue;
}
}
可以使用try…函数中的Catch块:
public function executeThatFunction($someVariable) {
try {
// do something that may craches
} catch($e)
{
// handle your error here
}
}