43 lines
939 B
Plaintext
43 lines
939 B
Plaintext
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.
|
|
|
|
|
|
----
|
|
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.
|
|
|
|
----
|
|
try {
|
|
/* ... */
|
|
} catch (MyException e) { // Compliant - something is done before throwing again the exception
|
|
fixSomething();
|
|
throw e;
|
|
}
|
|
----
|
|
|
|
|