les*_*gar 7 php exception try-catch
假设我有一个父类:
class Parent {
//...
}
Run Code Online (Sandbox Code Playgroud)
和一个方法的子类:
class Child extends Parent {
public function foo() {
// process and return data
}
public function bar() {
// process and return data
}
// way more methods...
}
Run Code Online (Sandbox Code Playgroud)
那么在PHP中有一种通用的方法来处理子方法中抛出的任何异常吗?例如,Parent类中的处理程序?或者我是否需要将所有方法体包装在单独的try-catch块中?
我想要实现的是,array()如果任何子方法抛出任何类型的异常,则返回空.
小智 -1
根据我的个人经验,创建自定义的异常处理程序并在收到该异常时返回空数组。这些链接将帮助您了解 PHP 中的异常处理: https://www.w3schools.com/php/php_exception.asp http://php.net/manual/en/language.exceptions.php