Rule:
S2142 “InterruptedException” should not be ignored
Environment:
SonarQube Version: 10.0.0.68432
Description:
This code should not raise S2142 but it does (InterruptedException is rethrown properly):
public void catchUnionType () {
try {
while (true) {
// do stuff
}
} catch (InterruptedException | java.io.IOException e) { // FP: S2142 here
unknownField.log(Level.WARN, "Interrupted!", e);
// clean up state...
throw e;
}
}
This patch (against https://github.com/SonarSource/sonar-java/@132163c) makes the tests fail:
diff --git a/java-checks-test-sources/src/main/files/non-compiling/checks/InterruptedExceptionCheck.java b/java-checks-test-sources/src/main/files/non-compiling/checks/InterruptedExceptionCheck.java
index 71d08c714..249546541 100644
--- a/java-checks-test-sources/src/main/files/non-compiling/checks/InterruptedExceptionCheck.java
+++ b/java-checks-test-sources/src/main/files/non-compiling/checks/InterruptedExceptionCheck.java
@@ -46,6 +46,15 @@ public class InterruptedExceptionCheck {
} catch (InterruptedException | java.io.IOException e) { // Noncompliant [[sc=14;ec=58]] {{Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.}}
unknownField.log(Level.WARN, "Interrupted!", e);
}
+ try {
+ while (true) {
+ // do stuff
+ }
+ } catch (InterruptedException | java.io.IOException e) {
+ unknownField.log(Level.WARN, "Interrupted!", e);
+ // clean up state...
+ throw e;
+ }
}
public void run () throws InterruptedException{