Findbugs Obl_Unsatisfied_Obligation

I'm trying to find bugs in on of our legacy code using findBugs. In one the methods, findBugs is giving OBL_UNSATISFIED_OBLIGATION error. I have verified that all streams are properly closed. Here is the code snippet:

FileWriter fw = null;
FileReader fr = null;
try {
    if (!new File(filePath).exists()) {
        requiredStrings = CommandUtils.invoke(filename);
        fw = new FileWriter(filePath);
        fw.write(requiredStrings);
    } else {               
        StringBuilder sb = new StringBuilder();
        fr = new FileReader(filePath);

        char[] buffer = new char[BLOCK_READ_SIZE];
        int bytesRead;
        while (-1 != (bytesRead = fr.read(buffer, 0, BLOCK_READ_SIZE))) {
            sb.append(buffer, 0, bytesRead);
        }
        requiredStrings = sb.toString();
    }
} finally {
    if (fw != null) {
        fw.close();
    }
    if (fr != null) {
        fr.close();
    }
}
return requiredStrings;

The error says that Obligation to clean up resurces in not discharged, Path continues at ....line.... Remaining obligations {Reader x 1, Writer x-1}

2

2 Answers

You have to catch IO exceptions throwed by FileReader and FileWriter when they are closing. You can do that in Java 7 and upper with try with resources

try (FileWriter fw = new FileWriter(filePath); FileReader fr = new FileReader(filePath)) {
    /*your code here*/
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Or by the old way

    FileWriter fw = null;
    FileReader fr = null;
    try {
        /*your code here*/
        fw = new FileWriter(filePath);
        /*your code here*/
        fr = new FileReader(filePath);
        /*your code here*/
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fw != null) {
                fw.close();
            }
            if (fr != null) {
                fr.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In a method, it opens a InputStream for reading, if there is a chance the method quits without closing this InputStream object, FindBugs would complain like fail to clean up java.io.InputStream on checked exception. For example:

void readProperties() throws FooException{
    InputStream is= ...
    PropertyFactory.getInstance().loadXXXFromPropertyStream(is); // it throws FooException
    is.close(); // maybe never called for a FooException leaving inputstream is open.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest