小编Hir*_*ala的帖子

最后可以使用try catch吗?

我正在使用缓冲的编写器和我的代码,关闭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

java exception-handling

22
推荐指数
2
解决办法
2万
查看次数

标签 统计

exception-handling ×1

java ×1