Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Findbugs: RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

Tags:

java

io

findbugs

I am using FindBugs to analyze my code in Eclipse from Ant.

Following snippet gives RV_RETURN_VALUE_IGNORED_BAD_PRACTICE:

RV: Method ignores exceptional return value (RV_RETURN_VALUE_IGNORED_BAD_PRACTICE)

This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.

public void export (File file) throws IOException {
    if (!file.exists()) {
        file.createNewFile();
    }

    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
    ...

Actually I don't care where file existed or not, the method should keep executing. If exception happened it would be thrown outside of export()

How can I rewrite this snippet, so warning/error is not shown, without disabling it in Findbugs config file?

like image 823
Nikolay Kuznetsov Avatar asked Jan 14 '23 00:01

Nikolay Kuznetsov


1 Answers

In that particular case, you don't have to call file.createNewFile() because the file will be created anyway by FileWriter.

However, you must assure that the parent folder for the file exists.

like image 170
Danubian Sailor Avatar answered Jan 19 '23 10:01

Danubian Sailor