1

I am trying to catch a custom exception, but keep getting an Exception 'CustomException' is never thrown in the corresponding try block inspection error for the following scenario:

class CustomException extends Exception { }

class Test {

    /**
     * @throws CustomException
     */

    public function willFail()
    {
        throw new CustomException('I failed');
    }

}

$test = new Test;

try {

    $test->willFail();

} catch (CustomException $e) {

    echo $e->getMessage();

}

The above example works exactly as expected, aside from PhpStorm giving me the inspection error.

I've searched everywhere and can't find out how to fix this without disabling the inspection entirely. Any help would be greatly appreciated!

Screenshot2

Screenshot

MultiDev
  • 10,389
  • 24
  • 81
  • 148
  • works fine on my box. What do you mean 'Inspection error' ? – YvesLeBorg Jul 27 '20 at 02:34
  • @YvesLeBorg I've added a screenshot to my question. I hope that helps. Thanks! – MultiDev Jul 27 '20 at 02:35
  • not able to duplicate : PhpStorm 2020.1.4, osx, php 7.2.8 – YvesLeBorg Jul 27 '20 at 02:41
  • of course, there is always the jetbrains classic for every bizarro you will encounter. Reindex and restart. – YvesLeBorg Jul 27 '20 at 02:52
  • @YvesLeBorg Thanks for checking. I just updated my version of PhpStorm to the same version as you 2020.1.4, and I am still experiencing this issue... strange... – MultiDev Jul 27 '20 at 03:01
  • Looks like PhpStorm is failing to read your PHPDoc. If you hover over the method name (where it's defined), does it show that it throws that exception (on the tooltip)? – Jeto Jul 27 '20 at 04:14
  • @Jeto Yes, it does show that it throws that exception on the tooltip. I've also updated the screenshot above and the example code, which is the entirety of the file in question. – MultiDev Jul 27 '20 at 04:47

1 Answers1

2

Just in case anyone else runs into this problem themselves in the future, I'm posting the solution to this problem.

This was fixed by updating "Call tree analysis depth" to "1" in Preferences > Languages & Frameworks > PHP. Click on the "Analysis" tab and update the setting there.

Screenshot

MultiDev
  • 10,389
  • 24
  • 81
  • 148