rspec/rules/S2147/rule.adoc
2022-02-04 16:28:24 +00:00

41 lines
776 B
Plaintext

Since Java 7 it has been possible to catch multiple exceptions at once. Therefore, when multiple ``++catch++`` blocks have the same code, they should be combined for better readability.
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++7++``.
== Noncompliant Code Example
[source,text]
----
catch (IOException e) {
doCleanup();
logger.log(e);
}
catch (SQLException e) { // Noncompliant
doCleanup();
logger.log(e);
}
catch (TimeoutException e) { // Compliant; block contents are different
doCleanup();
throw e;
}
----
== Compliant Solution
[source,text]
----
catch (IOException|SQLException e) {
doCleanup();
logger.log(e);
}
catch (TimeoutException e) {
doCleanup();
throw e;
}
----