Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
V6092. A resource is returned from...
menu mobile close menu
Additional information
toggle menu Contents

V6092. A resource is returned from try-with-resources statement. It will be closed before the method exits.

21 Aoû 2020

The analyzer has detected a situation where an 'AutoCloseable' object used in a try-with-resources statement is being returned from a method.

The try-with-resources statement automatically closes all resources when exiting – that is, the resource will always be already closed when it's returned. In most cases, closed resources have no use, and calling their methods will almost always lead to an 'IOException'.

public InputStream getStreamWithoutHeader() throws IOException
{
  try (InputStream stream = getStream())
  {
    stream.skip(HEADER_LENGTH);
    return stream;
  }
}

In this case, 'stream' will be closed before the control is passed to the calling method, and it will be impossible to use this stream in any way.

Fixed code:

public InputStream getStreamWithoutHeader() throws IOException
{
  InputStream stream = getStream();
  stream.skip(HEADER_LENGTH);
  return stream;
}