【发布时间】:2018-03-21 17:51:23
【问题描述】:
我正在使用 PhpStorm,并且我在我有一个实例的子类的父类中抛出了一个自定义异常。
我没有从子类中的父调用中捕获异常,因为我希望将其作为对子类实例进行调用的代码的责任。
PhpStorm 抱怨捕获的异常没有在 try 块中抛出,但是父级的方法确实抛出了它,这个方法是从 try 块中调用的子方法调用的。
这是检查员的错误还是我真的在这里做错了什么?
下面是一些复制问题的示例代码:
<?php
class testE extends \Exception {
}
class parentClass {
public function testMethod() {
throw new testE('test exception');
}
}
class childClass extends parentClass {
public function doSomething() {
$this->testMethod();
}
}
$test = new childClass;
try {
$test->doSomething();
} catch(testE $e) {
// ^--- why does this report no throw in try?
// Exception 'testE' is never thrown in the corresponding try block
// Will this still work even though phpstorm complains?
}
这是一张照片
【问题讨论】:
-
IDE 在抛出的异常深度为 2 级时依赖注释。没有注释只检查第一级。因此,如果您将
$test->doSomething();替换为$test->testMethod();,则不会报告此类问题(因为您将直接调用testMethod()(第一级))。使用建议的解决方案查看答案。
标签: php exception-handling phpstorm