Why such an example is performed without issuing any exceptions.

PrintWriter os = new PrintWriter(System.out, true); os.close(); os.println("text"); 
  • Judging by the source code, you should have a NullPointerException error. What makes you think that there is no error? - Andrew Bystrov
  • @AndrewBystrov, launched and tested - jisecayeyo
  • Give a minimalistic and working example of code that demonstrates this behavior. ru.stackoverflow.com/help/mcve - Andrew Bystrov
  • @AndrewBystrov, PrintWriter os = new PrintWriter(System.out, true); os.close(); os.println("text"); - jisecayeyo

1 answer 1

PrintWriter not an ordinary class; if you look at the documentation for the class, you will see the following:

Methods in this class never throw I / O exceptions, although some of its constructors may. The client may inquire {@link #checkError checkError ()}.

Those. it swallows all I / O errors.