43 lines
804 B
Plaintext
43 lines
804 B
Plaintext
== Why is this an issue?
|
|
|
|
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;
|
|
}
|
|
----
|
|
|
|
|