Search code examples
javasonarqubetry-catch-finallysonarlintfalse-positive

SonarQube, jump statements in finally block (squid:S1143)


I know, jump statements in finally block should not be used. In this simple example 'break' is used to break the 'switch'. SonarQube (5.6.3) with sonar-java 4.5.0.8398 reports an issue on:

"Jump statements should not occur in "finally" blocks (squid:S1143)"

public static void breakInFinallyIssue(){
    int a = 0;
    try{
        a = 1 / 0;
    }catch(Exception x){
        System.out.println("div by zero");
    }
    finally{
        switch (a) {
        case 0:
            //do something
            break;
        default:
            break;
        }
        //do something more
    }
}

Is this a known FP/bug?


Solution

  • You are right that this is a false positive. However such complex logic doesn't belong to the finally block, and if possible should be extracted to aptly named cleanup method. This will not only shutdown the warning, but also improve readability of your code.