Java io ugly try-finally block

Posted by Tom Brito on Stack Overflow See other posts from Stack Overflow or by Tom Brito
Published on 2010-04-23T14:12:42Z Indexed on 2010/04/23 14:13 UTC
Read the original article Hit count: 273

Filed under:
|

Is there a not so ugly way of treat the close() exception to close both streams then:

    InputStream in = new FileInputStream(inputFileName);
    OutputStream out = new FileOutputStream(outputFileName);

    try {
    copy(in, out);
    } finally {
    try {
        in.close();
    } catch (Exception e) {
        try {
        // event if in.close fails, need to close the out
        out.close();
        } catch (Exception e2) {}
        throw e; // and throw the 'in' exception
    }
    out.close();
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about file-io