【发布时间】:2019-06-02 09:37:15
【问题描述】:
我想将旧代码转换为使用异常,但它不应该在抛出异常时停止。这是旧代码:
function update()
{
foreach ($this->instances as $id => $instance)
{
if ($instance->exists())
{
if (!$instance->save())
{
$this->setError($instance->getError());
continue;
}
continue;
}
}
如果我想使用 try catch 块,我需要使用 continue 来避免脚本停止吗?这就是 try-catch 的代码:
function update()
{
foreach ($this->instances as $id => $instance)
{
if ($instance->exists())
{
try
{
$instance->save();
}
catch (exception $e)
{
echo $e->getMessage();
}
}
}
【问题讨论】: