0

I'm trying to understand why printing to a closed PrintStream does not display a stream closed exception, the way FileOutputStream does.

For example, the following code does not display an exception on Eclipse's console:

public class Try {
    public static void main(String[] args) {
        try {
            PrintStream printStream = new PrintStream(System.out);
            printStream.close();
            printStream.println("ABC");
        } catch (Throwable t) {
            System.err.println("Throwable caught");
        }
    }
}

Is it because the exception is happening on a separate thread? Or perhaps is PrintStream.Println defined not to throw an exception?

Edit: Non of PrintStream's methods do. Even the ones inherited from FileOutputStream, which do throw exceptions. Possibly a violation of LSP.

Gonen I
  • 5,576
  • 1
  • 29
  • 60
  • 1
    The second one. [`PrintStream.println(String)`](https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(java.lang.String)) doesn't throw an `Exception`. – Elliott Frisch Sep 03 '16 at 21:53
  • So apparently none of PrintStream's methods do. They all set an internal error flag instead. – Gonen I Sep 03 '16 at 22:17

0 Answers0