
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
78 lines
2.2 KiB
Plaintext
78 lines
2.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
An exception in a ``++throws++`` declaration in Java is superfluous if it is:
|
|
|
|
* listed multiple times
|
|
* a subclass of another listed exception
|
|
* completely unnecessary because the declared exception type cannot actually be thrown
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
void foo() throws MyException, MyException {} // Noncompliant; should be listed once
|
|
void bar() throws Throwable, Exception {} // Noncompliant; Exception is a subclass of Throwable
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
void foo() throws MyException {}
|
|
void bar() throws Throwable {}
|
|
----
|
|
|
|
|
|
=== Exceptions
|
|
|
|
The rule will not raise any issue for exceptions that cannot be thrown from the method body:
|
|
|
|
* in overriding and implementation methods
|
|
* in interface ``++default++`` methods
|
|
* in non-private methods that only ``++throw++``, have empty bodies, or a single return statement.
|
|
* 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
|
|
|
|
Also, the rule won't raise issues on ``++RuntimeException++``, or one of its descendants, because explicating runtime exceptions which could be thrown can ultimately help the method's users, and can even be considered as good practice.
|
|
|
|
[source,java]
|
|
----
|
|
class A extends B {
|
|
@Override
|
|
void doSomething() throws IOException {
|
|
compute(a);
|
|
}
|
|
|
|
public void foo() throws IOException {}
|
|
|
|
public void qix() throws MyRuntimeException {}
|
|
|
|
protected void bar() throws IOException {
|
|
throw new UnsupportedOperationException("This method should be implemented in subclasses");
|
|
}
|
|
|
|
Object foobar(String s) throws IOException {
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @throws IOException Overriding classes may throw this exception if they print values into a file
|
|
*/
|
|
protected void print() throws IOException { // no issue, method is overridable and the exception has proper javadoc
|
|
System.out.println("foo");
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 15 Jul 2013, 08:25:53 Dinesh Bolkensteyn wrote:
|
|
Implemented by \http://jira.codehaus.org/browse/SONARJAVA-210
|
|
|
|
endif::env-github,rspecator-view[]
|