Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

phpstorm

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?

like image 745
blacktemplar Avatar asked Dec 03 '17 12:12

blacktemplar


People also ask

What happens when an unhandled exception occurs?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

Which event is used for unhandled exceptions?

asax and the Application_Error event handler to execute code when an unhandled exception occurs. Specifically, we used this event handler to notify a developer of an error; we could extend it to also log the error details in a database.


1 Answers

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

like image 184
pa3py6aka Avatar answered Sep 28 '22 11:09

pa3py6aka