我正在使用缓冲的编写器和我的代码,关闭finally块中的编写器.我的代码是这样的.
...........
BufferedWriter theBufferedWriter = null;
try{
theBufferedWriter =.....
....
......
.....
} catch (IOException anException) {
....
} finally {
try {
theBufferedWriter.close();
} catch (IOException anException) {
anException.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
最后我必须在清理代码中使用try catch,因为theBufferedWriter也可能抛出IOException.我不想把这个异常抛给调用的方法.最后使用try catch是一个好习惯吗?如果不是什么替代方案?请建议.
问候,Hiral