gkephorus
gkephorus

Reputation: 1386

SonarLint not picking up exception throwing inside "instance initializer block" of an anonymous class

When I have this kind of Java code:

@Test
void testExplainJavaS1130_WithAnonymousInstanceInitilizer() throws Exception {
    var something = new IllegalStateException() {
        private static final long serialVersionUID = 1L;

        {
            // This is what is throwing but Java:S1130 does not see it
            throwingMethod();
        }

        private void throwingMethod() throws Exception {
            throw new Exception("On purpose for testing");
        }
    };
    assertNotNull(something);
}

I get this message from SonarLint:

Remove the declaration of thrown exception 'java.lang.Exception', as it cannot be thrown from method's body.

Now I could "deactivate rule java:S1130" but it would normally help me nicely. So I would like the SonarLint system to pick up the actual Exception throwing inside of the "instance initializer block". How do I stimulate SonarLint to do this?

I could live with turning of this rule in test-scope, any help with that would also be welcome.

I'm using "SonarLint for Eclipse: 10.9.1.82333".

PS this indeed is related to the old jmock system I still enjoy using.

Upvotes: 0

Views: 38

Answers (1)

Esteve Quenum
Esteve Quenum

Reputation: 1

Essaye ce code

var something = new IllegalStateException() {
    private static final long serialVersionUID = 1L;

    {
        try {
            throwingMethod();
        } catch (Exception e) {
            throw new RuntimeException("Exception during instance initialization", e);
        }
    }

    private void throwingMethod() throws Exception {
        throw new Exception("On purpose for testing");
    }
};

Upvotes: 0

Related Questions