java:S1989 FP with nested try/catch blocks

Hi,

java:S1989 rule (“Exceptions should not be thrown from servlet methods”) seems to raise a FP on nested try/catch blocks.

Here’s a code snippet to reproduce the issue:

import java.io.IOException;
import java.io.Writer;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    {

        try (Writer writer = response.getWriter()) {
            try {
                int[] myNumbers = {1, 2, 3};
                System.out.println(myNumbers[10]);
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Something went wrong.");
            }
            writer.write("Just writing stuff.");
        } catch (IOException e) {
            System.out.println("IO error.");
        }

    }

}

This snippet raises the issue “Handle the following exception that could be thrown by “write”: IOException.” on writer.write() method, even though IOException is catched.
If we remove the nested try/catch block, the issue is not raised.

We are using SonarQube Enterprise Edition v10.6 (92116).

Can someone please look into this?

Thanks.

Hi @BloodyMary

Welcome to the community!

Thank you for reporting the false positive and the short reproducer :pray:.

I have created a ticket to track the issue.

Best,
Erwan

1 Like