简体   繁体   English

SonarQube,finally块中的跳转语句(squid:S1143)

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

I know, jump statements in finally block should not be used. 我知道,不应该使用finally块中的跳转语句。 In this simple example 'break' is used to break the 'switch'. 在这个简单的例子中,'break'用于打破'switch'。 SonarQube (5.6.3) with sonar-java 4.5.0.8398 reports an issue on: SonarQube(5.6.3)与sonar-java 4.5.0.8398报告了一个问题:

"Jump statements should not occur in "finally" blocks (squid:S1143)" “跳转语句不应出现在”finally“块中(鱿鱼: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? 这是一个已知的FP / bug吗?

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. 但是这种复杂的逻辑并不属于finally块,如果可能的话应该提取到恰当命名的清理方法。 This will not only shutdown the warning, but also improve readability of your code. 这不仅会关闭警告,还会提高代码的可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM