微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PhpStorm检查错误或错误代码?在try块中未引发异常是意外的

我正在使用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?
}

这是一张照片

screenshot

解决方法:

如有疑问,请使用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突然理解了您的代码,如下所示:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐