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?
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.
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.
From phpStorm version 2018.1 you can exclude any exceptions from analysis. Go to preferences->Languages & Frameworks->PHP
and open Analysis
tab.
Here you can add exceptions to Unchecked Exceptions
list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With