1

Hi I was going through IO API and got something unexpected, Surely there is reason behind it which I don't know.Can any one tell me that why PrintWriter class's write() method implemented in such a way that it is eating up an exception ?

Below code is not throwing "IOException : Stream closed" because it is handled in write() method while it is thrown by ensureOpen() method.

pw = new PrintWriter(file);
pw.write("Hello\n");
pw.flush();
pw.close();

pw.write("World");
pw.flush();

But for FileWriter class, After closing FileWriter object, Again if you try to use its write() method it will through "java.io.IOException: Stream closed" !

user207421
  • 305,947
  • 44
  • 307
  • 483
Ravi Jiyani
  • 919
  • 1
  • 10
  • 26
  • 1
    [This](http://stackoverflow.com/questions/297303/printwriter-and-printstream-never-throw-ioexceptions) might help you. http://stackoverflow.com/questions/297303/printwriter-and-printstream-never-throw-ioexceptions – Parth Soni Mar 08 '15 at 13:11
  • @Parth : Thanks , Its exact what I was looking for ! – Ravi Jiyani Mar 08 '15 at 13:14

0 Answers0