Search code examples
phpphpstorm

PhpStorm: what to do with unhandled exceptions when preconditions are met


I really like the PhpStorm inspection tools. They help me a lot to write better code. Now I have the following situation and I am asking myself what's the best way to deal with such situations.

I have a function f with some precondition, for example like in the following code:

/**
 * @param int $x
 * @throws PreconditionException x is negative
 */
 public function f(int $x): int
 {
   if ($x < 0) {
     throw new PreconditionException("the input x is negative");
   }
 }

Then I use this function somewhere lets say the following:

f(5);

Now PhpStorm warns me with "Unhandled exception". But in this case I know that the exception will not be thrown, so adding a try block makes not really sense. Should I simply ignore this warning or what's the best way to deal with that?


Solution

  • From phpStorm version 2018.1 you can exclude any exceptions from analysis. Go to preferences->Languages & Frameworks->PHP and open Analysis tab.

    enter image description here

    Here you can add exceptions to Unchecked Exceptions list