在 Controller 类文件中,大多数方法函数都包含try/catch块,如下所示:
try
{
$stmt = $this->prepare($sql);
$stmt->execute($params);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
//foreach() or so on...
}
catch (Exception $e)
{
//bunch of code...
//save error into database, etc.
//error into json and pass to view file
}
在catch块中有很多代码,有没有一种减少代码的方法。是否可以在catch块中添加“抛出异常”?
最佳答案
是的。自己尝试。您始终可以在catch块中抛出新的Exception或重新抛出相同的异常。
try
{
// ...
}
catch (Exception $e)
{
// do whatever you want
throw new Your_Exception($e->getMessage());
// or
throw $e;
}
关于php - PDO的try/catch block 过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620305/