0

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.

1
  • If you spot a bug or if you have a feature request, please do not ask a question on Stack Overflow but report it directly to the project, best with a pull request, especially if it is an open source project you are using for free. The rule Java:S1130 seems to be implemented in RedundantThrowsDeclarationCheck.java, see also RedundantThrowsDeclarationCheckTest.java. Commented Dec 3, 2024 at 8:26

1 Answer 1

0

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");
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.