#562 New Check: Illegal throwing.

Future
closed
nobody
Check (274)
5
2013-11-17
2011-02-15
Anonymous
No

In our team it is considered a bad practice to throw NullPointerException ,and we should use IllegalArgumentException and IllegalStateException instead. The request it to make a configurable check to forbid not only declaring certain types of exceptions, but also throwing it.

We use the following piece of configuration inside the module TreeWalker:
<module name="Regexp">
<property name="format" value="throw new NullPointerException"/>
<property name="message" value="Please throw IllegalArgumentException or IllegalStateException instead of NullPointerException!"/>
<property name="illegalPattern" value="true"/>
</module>

However we are concerned about efficiency of such way, it is not very convenient to alter this check, it raises warnings for commented code (it is a bad practice on its own, but it is a separate bad practice) and it do not raise warnings for something like this:
NullPointerException ex = new NullPointerException();
throw ex;

Discussion

  • Roman Ivanov
    Roman Ivanov
    2011-02-18

    it will be useful to detect even this:
    try {
    } catch(NullPointerException ex) {
    throw ex;
    }

     
  • Ivan Sopov
    Ivan Sopov
    2013-11-17

    • status: pending --> closed
    • Group: --> Future