rspec/rules/S1164/rule.adoc

47 lines
995 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
Catching an exception only to immediately rethrow it without doing anything else is useless and misleading.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[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;
}
----