Is it okay to use try catch inside finally?

Posted by Hiral Lakdavala on Stack Overflow See other posts from Stack Overflow or by Hiral Lakdavala
Published on 2010-06-16T07:07:40Z Indexed on 2010/06/16 7:12 UTC
Read the original article Hit count: 291

Filed under:
|
|

Hi,

I am using a buffered writer and my code, closes the writer in the finally block. My code is like this.

 ...........
    BufferedWriter theBufferedWriter = null;
    try{
    theBufferedWriter =.....
    ....
    ......
    .....
    } catch (IOException anException) {
    ....
    } finally {
        try {
            theBufferedWriter.close();              
        } catch (IOException anException) {
            anException.printStackTrace();
            }
    }

I have to use the try catch inside the clean up code in finally as theBufferedWriter might also throw an IOException. I do not want to throw this exception to the calling methos. Is it a good practice to use a try catch in finally? If not what is the alternative? Please suggest.

Regards, Hiral

© Stack Overflow or respective owner

Related posts about java

Related posts about design