习题答案是没有加粗的删除部分;用不用加上?
加上后,nc1在1/0抛异常时就释放了资源,还是说因为在构造方法中,构造方法失败,FailingConstructor23b 对象不存在,就谈不上释放FailingConstructor23b中的nc1资源
public class FailingConstructor23b {
private BufferedReader in;
NeedsCleanup nc1;
NeedsCleanup nc2;
public FailingConstructor23b(String fname) throws Exception {
nc1 = new NeedsCleanup();
try {
in = new BufferedReader(new FileReader(fname));
//code might throw exceptions
int a = 1/0;
} catch(FileNotFoundException e) {
System.out.println("Could not find file " + fname);
nc1.dispose()
throw e;
} catch(Exception e) {
try {
in.close();
nc1.dispose()
} catch(IOException e2) {
System.out.println(“in.close() failed”);
}
throw e;
}
nc2 = new NeedsCleanup();
}
}