我正在使用缓冲写入器,我的代码在finally块中关闭写入器。我的代码如下。
...........
BufferedWriter theBufferedWriter = null;
try{
theBufferedWriter =.....
....
......
.....
} catch (IOException anException) {
....
} finally {
try {
theBufferedWriter.close();
} catch (IOException anException) {
anException.printStackTrace();
}
}
由于BufferedWriter可能会抛出IOException,因此我必须在finally中的清理代码中使用try-catch。我不想将此异常抛到调用方法中。在finally中使用try-catch是一个好习惯吗?如果不是,有什么替代方案吗?请建议。
谢谢, Hiral