How to close IOs?

Posted by blackdog on Stack Overflow See other posts from Stack Overflow or by blackdog
Published on 2012-11-05T04:50:14Z Indexed on 2012/11/05 4:59 UTC
Read the original article Hit count: 163

Filed under:
|

when i managed IO, i found a problem. i used to close it like this:

try {
        // my code
    } catch (Exception e) {
        // my code
    } finally{
        if (is != null) {
            is.close();
        }
    }

but the close method also would throw exception. if i have more than one IO, i have to close all of them. so the code maybe like this:

try {
    // my code
} catch (Exception e) {
    // my code
} finally{
    if (is1 != null) {
        is1.close();
    }
    if(is2 != null{
        is2.close();
    }
    // many IOs
}

if is1.close() throws an exception, is2, is3 would not close itself. So i have to type many try-catch-finally to control them. is there other way to solve the problem?

© Stack Overflow or respective owner

Related posts about java

Related posts about io