我正在使用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?
}
这是一张照片
解决方法:
如有疑问,请使用PhpStorm查看您的评论:
class testE extends \Exception
{
}
class parentClass
{
/**
* @throws testE <- added this
*/
public function testMethod()
{
throw new testE('test exception');
}
}
class childClass extends parentClass
{
/**
* @throws testE <- added this
*/
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?
}
瞧,突然间PhpStorm突然理解了您的代码,如下所示:
原文地址:https://codeday.me/bug/20191025/1927445.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。