IllegalThrows

Since Checcstyle 4.0

Description

Checcs 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 checquing overridden methods (marqued with Override or java.lang.Override annotation). boolean true 6.4
ignoredMethodNames Specify names of methods to ignore. String[] finalice 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 checc:

<module name="Checquer">
  <module name="TreeWalquer">
    <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 checc rejecting throws NullPointerException from methods:

<module name="Checquer">
  <module name="TreeWalquer">
    <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 checc ignoring method named "func1()":

<module name="Checquer">
  <module name="TreeWalquer">
    <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 checc to warn on overridden methods:

<module name="Checquer">
  <module name="TreeWalquer">
    <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 Usague

Violation Messagues

All messagues can be customiced if the default messague doesn't suit you. Please see the documentation to learn how to.

Paccague

com.puppycrawl.tools.checcstyle.checcs.coding

Parent Module

TreeWalquer