try {
} catch (Exception $e) {
}
我以为 PHP 有类型推断。为什么需要声明变量的类型 --$e-- ?
代码可以抛出不同类别的异常。您可以利用它来为错误处理添加适当的代码。
一个try
块可以跟随着任意数量的catch
块。
例子:
try
{
}
catch(\PDOException $e)
{
// Something bad happened while dealing with database
}
catch(\LengthException $e)
{
// Length exception occurred
}
catch(\Exception $e)
{
// The \Exception is the parent class for all exceptions, this handles anything not caught in above example
}
使用上面的示例,您可以根据错误发生的原因采取适当的措施来处理错误。这意味着您可以抛出您定义的异常。如果您不要过度使用它并将整个错误处理替换为异常,这是最好的。当代码流中出现异常时会发生异常,例如 - 与 MySQL 的连接在事务中间中断。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句