new JarFile(path)可以引发I / O异常。

如果发生这种情况,并且我发现了异常,是否应该关闭该异常? (我想真正的问题是,有什么要关闭的吗?)

另一个问题是:如果可行,我应该清理吗?即general rules for dealing with streams是否适用?

抱歉,这个问题有点麻烦了;我是刚接触JarFile的新手(过去也没有真正使用过流)。

最佳答案

如果new JarFile(path)抛出IOException,则没有任何可关闭的内容:

JarFile file = null;

try {
    file = new JarFile("does/not/exist");
} catch (IOException e) {
    System.out.println(file); //Prints out null
}

try {
    file.close(); //Throws NullPointerException
} catch (IOException e) {
    e.printStackTrace();
}

10-08 10:49