Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V6128. Using a Closeable object...
menu mobile close menu
Additional information
toggle menu Contents

V6128. Using a Closeable object after it was closed can lead to an exception.

May 20 2025

The analyzer has detected a method call on an object that had already been closed using the closemethod. This may result in an exception being thrown.

The example:

public void appendFileInformation(String path) throws IOException {
    FileInputStream is = new FileInputStream(path);
    // ....
    is.close();
    // ....
    if (is.available() == 0) {
        System.out.println("EOF");
    }
    // ....
}

The condition ensures that the file has been completely read. The check compares the number of remaining bytes to zero. However, since the close method is called on the is variable before, calling the available method results in an IOException with the message Stream Closed. That is because the resources held by is are already released.

Look at a proper implementation of appendFileInformation:

public void appendFileInformation(String path) throws IOException {
    try (FileInputStream is = new FileInputStream("out.txt")) {
        // ....
        if (is.available() == 0) {
            System.out.println("EOF");
        }
        // ....
    }
}

To ensure the method works correctly, consider using try-with-resources. In this case:

  • the resources held by the is object will be automatically released after the try block ends;
  • the object cannot be accessed outside the try block, which adds an extra layer of protection against IOException.
  • resources are properly released even if an exception occurs inside the try block.

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
close form
Free PVS‑Studio license for Microsoft MVP specialists
close form
To get the licence for your open-source project, please fill out this form
close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam