Java – is this a resource leak or a false positive?

Eclipse gives a warning about the "out" declaration Is this a false positive?

Random r = new Random();
try(PrintWriter out1 = new PrintWriter("one.txt");
    PrintWriter out2 = new PrintWriter("two.txt"))
{
  PrintWriter out = r.nextBoolean()?out1:out2;
  out.println("x");
}

P. S.: the warning is "resource leak: 'out' will never be closed"

Solution

This is a false positive All instances have been closed correctly

I turned off the resource - related warnings in eclipse a long time ago They are really unreliable because there are many "obviously" correct control flow paths that eclipse cannot recognize as "correct" without actual execution... Any non trivial code is destined to produce these false positives

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>