我倾向于写代码如下所示的很多:如何简化/重用这个异常处理代码
BufferedWriter w = null; // Or any other object that throws exceptions and needs to be closed
try {
w = new BufferedWriter(new FileWriter(file));
// Do something with w
} catch (IOException e) {
e.printStackTrace();
} finally {
if (w != null) {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
,通常需要抛出异常,需要关闭,对象和关闭它也可能抛出异常。
我想知道上面的代码是否可以简化或以任何方式重用。
我学习了很多的Java感谢这个问题。 :D – hpique 2010-10-11 20:11:22