
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.
47 lines
995 B
Plaintext
47 lines
995 B
Plaintext
== Why is this an issue?
|
|
|
|
Catching an exception only to immediately rethrow it without doing anything else is useless and misleading.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
try {
|
|
/* ... */
|
|
} catch (Exception e) { // Non-Compliant
|
|
throw e;
|
|
}
|
|
----
|
|
|
|
|
|
=== Exceptions
|
|
|
|
When all instances of a general exception must be handled, but some specific ones not, propagation must be used and so is allowed by this rule.
|
|
|
|
|
|
[source,text]
|
|
----
|
|
try {
|
|
/* ... */
|
|
} catch (RuntimeException e) { // Compliant - propagation of the unchecked exception
|
|
throw e;
|
|
} catch (Exception e) { // Compliant - catching of the checked exception
|
|
LOGGER.error("...", e);
|
|
}
|
|
----
|
|
|
|
Throwing the same exception can also makes sense when an action is done before throwing it again.
|
|
|
|
[source,text]
|
|
----
|
|
try {
|
|
/* ... */
|
|
} catch (MyException e) { // Compliant - something is done before throwing again the exception
|
|
fixSomething();
|
|
throw e;
|
|
}
|
|
----
|
|
|
|
|