False Positive Java squid:S1141 (Try-catch blocks should not be nested) when used in a callback

593 views
Skip to first unread message

Paul Wagland

unread,
Nov 10, 2016, 4:43:07 PM11/10/16
to SonarQube
Hi all,

As always, great product… despite my level of feedback, I assure you that I am truly impressed with the capabilities of SonarQube.

As we slowly go through our code base looking into the raised issues, I am posting through some of the false positives that we are finding that I think probably should be fixed in SonarQube. The latest that I have found:


package org.kungfoocoder.sonarqube.bugs;


public class FalsePositiveNestedTry {

interface CallBack {

void callback() throws Exception;

}

void useCallback(CallBack callback) throws Exception {

callback.callback();

}

void test () {

try {

useCallback(new CallBack() {

@Override

public void callback() throws Exception {

try { // ← False Positive squid:S1141 (Try-catch blocks should not be nested) 

Thread.sleep(1000);

} catch (InterruptedException ie) {

throw new RuntimeException(ie);

}

}

});

} catch (Exception e) {

System.out.println("This must be here for a reason, right? msg: " + e.getMessage());

}

}

}



Given the above, it shouldn't flag a false positive in the callback, since it is in its own method, and so the exception can't get confused as to which level it should be caught at.

Of course, you can argue that the callback could be defined out of line, but that is not always practical/sensible anyway.

Cheers,
Paul

Michael Gumowski

unread,
Nov 11, 2016, 11:47:13 AM11/11/16
to Paul Wagland, SonarQube
Hey Paul,

Thanks again for the nice feedback (really appreciated!) and the reproducer for the issue.
It's indeed a FP with S1141. We should not count these try-catch statements as being nested when part of methods declared in anonymous classes.

I created the following JIRA ticket to handle the issue: SONARJAVA-1948

Cheers,

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/6be47c41-33a9-43e5-b099-bb97cd147b80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com
Reply all
Reply to author
Forward
0 new messages