31

I am wondering if the below code closes InputStream in finally block correctly

InputStream is = new FileInputStream("test");
try {
    for(;;) {
        int b = is.read();
        ...
    }
} finally {
    try {
        is.close();
    } catch(IOException e) {
    }
}

If an exception happens during is.read() will be it ignored / suppressed if an exception happens during is.close()?

2

9 Answers 9

17

Best way is to use Java 7 and use try with resources, or do same thing manualy and add exception from closing as suppressed exception.

Pre Java 7: If you are throwing your custom exception, you can add in it supressed exception like it is done in Java 7 (in your exception create fields List suppressed and put there exceptions from close operation and when dealing with your exception, look there too. If you cannot do that, I don't know anything better than just log it.

examples: from Java tutorials

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}

but better form is:

static String readFirstLineFromFile(String path) throws IOException {
    try (FileReader fr = new FileReader(path);
         BufferedReader br = new BufferedReader(fr)) {
        return br.readLine();
    }
}

This way even if creation of FileReader is succesfull but creation of BufferedReader fails (eg not enough memory), FileReader will be closed.

1
15

You can close it with IOUtils from https://commons.apache.org/proper/commons-io/

public void readStream(InputStream ins) {
    try {
        //do some operation with stream         
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        IOUtils.closeQuietly(ins);
    }
}
1
  • 5
    DON'T USE, deprecated and removed without replacement on IOUtils 2.6. "Please use the try-with-resources statement or handle suppressed exceptions manually."
    – Sergio
    Feb 12, 2018 at 15:31
8

The Java 6 specs say

If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice: If the finally block completes normally, then the try statement completes abruptly for reason R. If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

So you are right, you will lose the original exception.

The solution probably is to write your finally block so defensively that it is a bigger surprise (worth propagating) if the finally block fails than if an exception comes out of the try catch block.

So, for example, if it is possible that the stream may be null when you try to close it, check it:

InputStream is = new FileInputStream("test");
try {
    for(;;) {
        int b = is.read();
        ...
    }
} finally {
    try {
        if( is!=null ) {
            is.close();
        }
    } catch(IOException e) {
    }
}

In Java 7, Alpedar's solution is the way to go of course.

2
  • is = null is impossible, new FileInputStream("test") cannot return null Apr 23, 2013 at 15:08
  • It's meant to be a simple example, and it'd depend on what happens in the .... Was looking for a runtime exception that may get thrown in the finally block cause that's when things get interesting.
    – flup
    Apr 23, 2013 at 17:51
3

The exception from is.close() will be suppressed and the exception from is.read() will be the one that propagates up.

4
  • True, but only if the exception from is.close() is an IO exception. Apr 23, 2013 at 9:03
  • often it is a NullpointerException, when chaining streams, and the first stream throws a FileNotFpundException
    – AlexWien
    Apr 23, 2013 at 9:53
  • @Alex In the example given, is cannot be null at the is.close call, because otherwise the constructor of FileInputStream will have throw an exception earlier like you stated. However that is outside of the exception block. I can only reply to the code as it is shown not code as we'd write it ourselves! @Etienne you are correct and in this example they both throw the same exception.
    – sksamuel
    Apr 23, 2013 at 15:14
  • @Etienne interesting observation, but then it can only be a RuntimeException (programming errors) which are not supposed to be caught Apr 24, 2013 at 2:48
2

With the code you posted:

  • If is.close() throws an IOException, it gets discarded and the original exception propagates.
  • If is.close() throws something else (a RuntimeException or an Error), it propagates and the original exception is discarded.

With Java 7, the correct way to close an InputStream without loosing the original exception is to use a try-with-resources statement:

try (InputStream is = new FileInputStream("test")) {
    for(;;) {
        int b = is.read();
        // ...
    }
}

Prior to Java 7, what you do is just fine, except you may want to catch all exceptions instead of just IOExceptions.

1

Based on your code sample if an exception occurs at the int b = is.read(); point, then the exception will be raised higher up the call chain.

Note though that the finally block will still execute and if the Inputstream invalid another exception will be thrown, but this exception will be "swallowed", which may be acceptable depending on your use case.

Edit:

Based on the title of your question, I would add that what you have is fine in my opinion. You may want to additionally add a catch block to explicitly handle (or perhaps wrap) any exception within the first try block, but it is also acceptable to let any IO exceptions raise up - this really depends on your API. It may or may not be acceptable to let IO exceptions raise up. If it is, then what you have it fine - if it isn't then you may want to handle/wrap the IO exception with something more suitable to your program.

4
  • The exception will not be swallowed, it will be propagated instead of the original exception.
    – flup
    Apr 23, 2013 at 8:51
  • If InputStream is null, then the code will raise an NPE that hides the original exception, but if the close() raises an IO exception, then that exception will be discarded and the original one will propagate. Apr 23, 2013 at 9:01
  • @EtienneMiret agreed, it will. But the answer says the NPE will get swallowed, which it won't.
    – flup
    Apr 23, 2013 at 9:05
  • is = null is impossible, new FileInputStream("test") cannot return null. Apr 23, 2013 at 15:06
0

How about the next solution:

InputStream is = new FileInputStream("test");
Exception foundException=null;
try {
    for(;;) {
        int b = is.read();
        ...
    }
} catch (Exception e){
  foundException=e;
}
finally {
    if(is!=null)
    try {
        is.close();
    } catch(IOException e) {
    }
}
//handle foundException here if needed
0

If an exception happens during is.read() will be it ignored / suppressed if an exception happens during is.close()?

Yes. You have a catch block for the exception in close() which does not re-throw the exception. Ergo it is not propagated or rethrown.

2
  • try { throw new IllegalArgumentException(); } finally {try { throw new IllegalStateException();} catch (Exception e2) {}} -- IllegalArgumentException is thrown Apr 23, 2013 at 11:02
  • @EvgeniyDorofeev What on earth are you talking about?
    – user207421
    Apr 24, 2013 at 10:27
0

This is the sample to help to understand your problem, if you declare the scanner in the try-catch block it will give compiler warning the resource is not closed. so either make it locally or just in try()

import java.util.InputMismatchException;
import java.util.Scanner;

class ScanInt {
public static void main(String[] args) {
    System.out.println("Type an integer in the console: ");

    try (Scanner consoleScanner = new Scanner(System.in);) {
        System.out.println("You typed the integer value: "
                + consoleScanner.nextInt());

    } catch (InputMismatchException | ArrayIndexOutOfBoundsException exception) {
        System.out.println("Catch Bowled");
        exception.printStackTrace();

    }
    System.out.println("----------------");
}
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.