我傾向於寫代碼如下所示的很多:如何簡化/重用這個異常處理代碼
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