2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
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.
2020-06-30 12:48:07 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++7++``.
2020-06-30 12:48:07 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:07 +02:00
----
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;
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:07 +02:00
----
catch (IOException|SQLException e) {
doCleanup();
logger.log(e);
}
catch (TimeoutException e) {
doCleanup();
throw e;
}
----