Java “奇怪”;资源泄漏:流从未关闭”;如果在循环中引发异常,请使用try with resources

Java “奇怪”;资源泄漏:流从未关闭”;如果在循环中引发异常,请使用try with resources,java,eclipse,warnings,compiler-warnings,try-with-resources,Java,Eclipse,Warnings,Compiler Warnings,Try With Resources,为什么即使我使用try with resources,Eclipse仍会对以下代码发出奇怪的“Resource leak:zin is never closed”警告: Path file = Paths.get("file.zip"); // Resource leak warning! try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(file))) { for (int i = 0; i < 5

为什么即使我使用
try with resources
,Eclipse仍会对以下代码发出奇怪的“Resource leak:zin is never closed”警告:

Path file = Paths.get("file.zip");
// Resource leak warning!
try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(file))) {
    for (int i = 0; i < 5; i++)
        if (Math.random() < 0.5)
            throw new Exception();
} catch (Exception e) {
    e.printStackTrace();
}
Mod#2:如果我保留
for
循环,但我删除了包装
ZipInputStream
,也不会发出警告:

// This is OK (no warning)
try (InputStream in = Files.newInputStream(file))) {
    for (int i = 0; i < 5; i++)
        if (Math.random() < 0.5)
            throw new Exception();
} catch (Exception e) {
    e.printStackTrace();
}

我使用Eclipse开普勒(4.3.1),但开普勒SR2(4.3.2)的结果也一样。

这似乎是Eclipse中的一个已知错误:

我只是被这件事咬了一口,并且在追踪者上投了我的票


更新:上述错误已在4.5 M7中解决。这将包括在Eclipse4.5(“Mars”)的最终版本中,该版本看起来将于2015-06-24发布。

我是这样假设的,但很高兴确认我不必更改逻辑或样式,这只是IDE中的一个bug。
// This is OK (no warning)
try (InputStream in = Files.newInputStream(file))) {
    for (int i = 0; i < 5; i++)
        if (Math.random() < 0.5)
            throw new Exception();
} catch (Exception e) {
    e.printStackTrace();
}
// This is also OK (no warning)
InputStream in = Files.newInputStream(file); // I declare to throw IOException
try (ZipInputStream zin = new ZipInputStream(in)) {
    for (int i = 0; i < 5; i++)
        if (Math.random() < 0.5)
            throw new Exception();
} catch (Exception e) {
    e.printStackTrace();
}