IllegalThrows
Since Checkstyle 4.0
Description
Checks that specified types are not declared to be thrown.
Declaring that a method throws
java.lang.Error
or
java.lang.RuntimeException
is almost never acceptable.
Properties
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 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="IllegalThrows"/>
</module>
</module>
Example:
public class Example1 {
// violation below, 'Throwing 'RuntimeException' is not allowed'
void f1() throws RuntimeException {}
void f2() throws Exception {}
void f3() throws Error {} // violation, 'Throwing 'Error' is not allowed'
void f4() throws Throwable {} // violation, 'Throwing 'Throwable' is not allowed'
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 {}
// violation below, 'Throwing 'NullPointerException' is not allowed'
void f5() throws NullPointerException {}
@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, 'Throwing 'Error' is not allowed'
void f4() throws Throwable {} // violation, 'Throwing 'Throwable' is not allowed'
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 {
// violation below, 'Throwing 'RuntimeException' is not allowed'
void f1() throws RuntimeException {}
void f2() throws Exception {}
void f3() throws Error {} // violation, 'Throwing 'Error' is not allowed'
void f4() throws Throwable {} // violation, 'Throwing 'Throwable' is not allowed'
void f5() throws NullPointerException {}
@Override // violation below, 'Throwing 'Error' is not allowed'
public String toString() throws Error {
String str = "";
return str;
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.coding