PhpStorm 检查错误或错误代码? try 块中未抛出的异常是意外的

PhpStorm inspection bug or bad code? Exception not thrown in try block is unexpected

我正在使用 PhpStorm,我在我有一个实例的子 class 的父 class 中抛出一个自定义异常。

我没有在子实例中捕获父调用的异常,因为我希望捕获它是对子实例 class 进行调用的代码的责任。

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 突然间理解了您的代码,如下所示: