#562 New Check: Illegal throwing.

Check (274)

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"/>

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;


  • 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

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks