【发布时间】:2012-12-07 17:42:26
【问题描述】:
我在这个 sn-p 中收到关于“s”和“p”的资源泄漏警告。这个警告有效吗?
try (StringWriter s = new StringWriter(); PrintWriter p = new PrintWriter(s)) {
p.print(InetAddress.getLocalHost().getHostName());
p.print('/');
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface i = e.nextElement();
System.out.println(i);
if (i.getHardwareAddress() == null || i.getHardwareAddress().length == 0)
continue;
for (byte b : i.getHardwareAddress())
p.printf("%02x", b);
return s.toString();
}
throw new RuntimeException("Unable to determine Host ID");
}
【问题讨论】:
-
Eclipse 的编译器可能出错?尝试在 try 块之外抛出异常
-
这也是我的怀疑,但是 try-with-resources 对我来说还是有点新,所以我想三重检查。
-
也许您也可以在 Netbeans 或 IntelliJ IDEA 中打开您的代码并查看他们所说的内容。
-
您使用的是什么 IDE?使用 Eclipse Juno Service Release 2,没有警告。
-
投票结束,因为这很可能是一个 Eclipse 错误,该错误已被修复 - 我在现代 Eclipse 中也没有看到这样的警告。
标签: java eclipse compiler-warnings java-7 try-with-resources