Warn java resource

Eclipse (Juno) says there are resource leak warnings in this example . Is it really? This happens when the point of throwing the exception is in a loop for

.

package kuni;

import java.io.FileWriter;
import java.util.Arrays;

public class ResourceWarn {
    public static void main(String[] args){

        try {
            FileWriter f = null;
            try{
                f = new FileWriter("test.txt");
                for(String s : Arrays.asList("a","b","c")){
                    if(s.equals("c"))throw new RuntimeException("aa"); //resource leak warn here
                    f.write(s);
                }
            }finally{
                try{
                    f.close();
                }catch(Exception ignore){
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

      

+3


source to share


3 answers


I think I know what Eclipse is complaining about.

        } finally {
            try {
                System.err.println("closing f");
                f.close();
            } catch(Exception ignore) {
            }
        }

      

The problem is println

!!!

Eclipse thinks the call System.err.println(...)

might throw an exception. If it does, then the call f.close()

will not happen .... ergo, leak.



You and I know this "can't happen" ... but the Eclipse code analyzer probably doesn't understand the special nature System.err

.

It is also possible that somewhere else in the code we did something that would lead to an error System.err.println(...)

; for example we could use System.setErr(...)

to replace a regular instance with an System.err

instance of some custom subclass PrintWriter

that throws an unchecked exception on the second Tuesday of every month.

Try to remove println

or move it after calling close

.

+2


source


No, you don't have a resource leak. I don't know what eclipse is complaining about.

The only comments I have regarding your code are:



  • It is easier to read if you include spaces between keywords and open / close curly braces.

  • You shouldn't throw an IOException in the finally block, rather you should register it and continue (as you do now). There are strange bugs that happen when cleanup code throws exceptions and logging religiously will save you extremely painful debugging sessions.

  • For a demo like this, printing a stack trace is fine, but in general you should log it and only print a short error message to stderr. Users panic when they see stacktraces, even when they are benign.

Other than that, the code looks good to me and none of the above indicates a resource leak.

+1


source


When you throw out a new one RuntimeException

, you immediately turn on the block catch

without being able to close yours FileWriter

. You need to close yours FileWriter

when selected RuntimeException

.

public static void main(String[] arg) {
    FileWriter f = null;
    try {
        f = new FileWriter("test.txt");
        for (String s : Arrays.asList("a", "b", "c")) {
            if (s.equals("c"))
                throw new RuntimeException("aa"); // resource leak warn here
            f.write(s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (f != null) {
                f.close();
            }
        } catch (Exception ignore) {
        }
    }
}

      

+1


source







All Articles