2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-31 17:20:51 +02:00
Superfluous exceptions within `throws` clauses have negative effects on the readability and maintainability of the code.
2023-06-06 16:33:23 +02:00
An exception in a `throws` clause is superfluous if it is:
2021-04-28 18:08:03 +02:00
* listed multiple times
* a subclass of another listed exception
2023-06-06 16:33:23 +02:00
* not actually thrown by any execution path of the method
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 18:08:03 +02:00
2023-06-09 16:36:32 +02:00
[source,java,diff-id=1,diff-type=noncompliant]
2021-04-28 18:08:03 +02:00
----
void foo() throws MyException, MyException {} // Noncompliant; should be listed once
void bar() throws Throwable, Exception {} // Noncompliant; Exception is a subclass of Throwable
2023-05-31 17:20:51 +02:00
void boo() throws IOException { // Noncompliant; IOException cannot be thrown
System.out.println("Hi!");
}
2021-04-28 18:08:03 +02:00
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 18:08:03 +02:00
2023-06-09 16:36:32 +02:00
[source,java,diff-id=1,diff-type=compliant]
2021-04-28 18:08:03 +02:00
----
void foo() throws MyException {}
void bar() throws Throwable {}
2023-05-31 17:20:51 +02:00
void boo() {
System.out.println("Hi!");
}
2021-04-28 18:08:03 +02:00
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 18:08:03 +02:00
The rule will not raise any issue for exceptions that cannot be thrown from the method body:
2023-05-31 17:20:51 +02:00
* in interface `default` methods
* in overriding and implementating methods
* in non-private methods that only `throw`, have empty bodies, or a single return statement.
2021-04-28 18:08:03 +02:00
* in overridable methods (non-final, or not member of a final class, non-static, non-private), if the exception is documented with a proper JavaDoc
2023-05-25 14:18:12 +02:00
[source,java]
2021-04-28 18:08:03 +02:00
----
2023-05-31 17:20:51 +02:00
interface MyInterface {
default void defaultMethod() throws IOException {
System.out.println("Hi!");
}
void doSomething() throws IOException;
}
class A implements MyInterface {
2021-04-28 18:08:03 +02:00
@Override
void doSomething() throws IOException {
2023-05-31 17:20:51 +02:00
System.out.println("Hi!");
2021-04-28 18:08:03 +02:00
}
2023-05-31 17:20:51 +02:00
public void emptyBody() throws IOException {}
2021-04-28 18:08:03 +02:00
2023-05-31 17:20:51 +02:00
protected void singleThrowStatement() throws IOException {
2021-04-28 18:08:03 +02:00
throw new UnsupportedOperationException("This method should be implemented in subclasses");
}
2023-05-31 17:20:51 +02:00
Object singleReturnStatement() throws IOException {
2021-04-28 18:08:03 +02:00
return null;
}
/**
* @throws IOException Overriding classes may throw this exception if they print values into a file
*/
2023-05-31 17:20:51 +02:00
protected void overridable() throws IOException { // no issue, method is overridable and the exception has proper javadoc
2021-04-28 18:08:03 +02:00
System.out.println("foo");
}
}
----
2023-05-31 17:20:51 +02:00
Also, the rule will not raise issues on `RuntimeException`, or one of its sub-classes, because documenting runtime exceptions which could be thrown can ultimately help users of the method understand its behavior.
[source,java]
----
class B {
int possibleDivisionByZero(int a, int b) throws ArithmeticException {
return a / b;
}
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 15 Jul 2013, 08:25:53 Dinesh Bolkensteyn wrote:
Implemented by \http://jira.codehaus.org/browse/SONARJAVA-210
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]