diff --git a/java-checks-test-sources/src/main/java/checks/InterruptedExceptionCheck.java b/java-checks-test-sources/src/main/java/checks/InterruptedExceptionCheck.java index 2b8ce0daa43..c18eddb548d 100644 --- a/java-checks-test-sources/src/main/java/checks/InterruptedExceptionCheck.java +++ b/java-checks-test-sources/src/main/java/checks/InterruptedExceptionCheck.java @@ -363,6 +363,29 @@ void bar() throws InterruptedException { } } + public void throwExceptionInTryDirectly() throws InterruptedException { + try { + throw new RuntimeException(); + } catch (Exception e) { // Compliant + } + + try { + throw new InterruptedException(); + } catch (Exception e) { // Noncompliant + } + + try { + InterruptedException ie = new InterruptedException(); + throw ie; + } catch (Exception e) { // Noncompliant + } + + try { + throw getInterruptedException(); + } catch (Exception e) { // Noncompliant + } + } + void falsePositivesSonarjava4406() { try { try { @@ -381,6 +404,15 @@ void falsePositivesSonarjava4406() { } catch (Exception e) { // Compliant, because inner try does not throw an InterruptedException } + try { + try { + throwsInterruptedException(); + } catch (InterruptedException ie) { // Compliant + throw new InterruptedException(); + } + } catch (Exception e) { // Noncompliant, because inner try throws an InterruptedException + } + try { try { throwsInterruptedException(); diff --git a/java-checks/src/main/java/org/sonar/java/checks/InterruptedExceptionCheck.java b/java-checks/src/main/java/org/sonar/java/checks/InterruptedExceptionCheck.java index 07ab8190505..a1861c875f5 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/InterruptedExceptionCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/InterruptedExceptionCheck.java @@ -179,6 +179,15 @@ public void visitTryStatement(TryStatementTree tryStatementTree) { scan(tryStatementTree.catches()); scan(tryStatementTree.finallyBlock()); } + + @Override + public void visitThrowStatement(ThrowStatementTree tree) { + // besides to method invocation, we also need to collect throw statements + if (isInterruptingExceptionExpression(tree.expression())) { + invocationTree.add(tree); + } + super.visitThrowStatement(tree); + } } private static class BlockVisitor extends BaseTreeVisitor {