Since Checkstyle 4.0
java.lang.Error
or
java.lang.RuntimeException
is almost never acceptable.
name | description | type | default value | since |
---|---|---|---|---|
ignoreOverriddenMethods | Allow to ignore checking overridden methods (marked with Override or java.lang.Override annotation). |
boolean | true |
6.4 |
ignoredMethodNames | Specify names of methods to ignore. | String[] | finalize |
5.4 |
illegalClassNames | Specify throw class names to reject. | String[] | Error, RuntimeException, Throwable, java.lang.Error, java.lang.RuntimeException, java.lang.Throwable |
4.0 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalThrows"/> </module> </module>
Example:
public class Example1 { void f1() throws RuntimeException {} // violation void f2() throws Exception {} void f3() throws Error {} // violation void f4() throws Throwable {} // violation void f5() throws NullPointerException {} @Override public String toString() throws Error { String str = ""; return str; } }
To configure the check rejecting throws NullPointerException from methods:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalThrows"> <property name="illegalClassNames" value="NullPointerException"/> </module> </module> </module>
Example:
public class Example2 { void f1() throws RuntimeException {} void f2() throws Exception {} void f3() throws Error {} void f4() throws Throwable {} void f5() throws NullPointerException {} // violation @Override public String toString() throws Error { String str = ""; return str; } }
To configure the check ignoring method named "func1()":
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalThrows"> <property name="ignoredMethodNames" value="f1"/> </module> </module> </module>
Example:
public class Example3 { void f1() throws RuntimeException {} void f2() throws Exception {} void f3() throws Error {} // violation void f4() throws Throwable {} // violation void f5() throws NullPointerException {} @Override public String toString() throws Error { String str = ""; return str; } }
To configure the check to warn on overridden methods:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalThrows"> <property name="ignoreOverriddenMethods" value="false"/> </module> </module> </module>
Example:
public class Example4 { void f1() throws RuntimeException {} // violation void f2() throws Exception {} void f3() throws Error {} // violation void f4() throws Throwable {} // violation void f5() throws NullPointerException {} @Override public String toString() throws Error { // violation String str = ""; return str; } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding