try {
} catch (Exception $e) {
}
我认为PHP有类型推断。为什么声明变量的类型是必要的 - $ e--?
答案 0 :(得分:0)
代码可以抛出不同类别的异常。您可以利用它来为错误处理添加适当的代码。
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的连接中断了事务。