File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IOException vs FileNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IOException vs FileNotFoundException" Watch "IOException vs FileNotFoundException" New topic

IOException vs FileNotFoundException

jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
Hi all,
Is there any particular reason why instantiating a new FileWriter throws an IOException while instantiating a new PrintWriter throws a FileNotFoundException?

edit: oh and also, why don't the close() and flush() methods throw an IOException in PrintWriter while they do in FileWriter and BufferedWriter ?
[ December 16, 2008: Message edited by: jean-gobert de coster ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I think FileWriter is low-level writer and designed very early of the java production, so developers would have decided to propagate the exception upto the use to handle explicitly, that's why flush(), close(), write() throws exception.

And PrintWriter is very high level writer, it is known for easy to use. That's why they would have handled these execeptions in the methods itself instead of propagating. I donot think there any big issue to design this way. I think first they propagated the exception, but after some time they could have found handling them will provide more cleaner API, so...

All IO methods have exception, but in some they are handled in them, in some they are thrown.

Any sheriff could tell us about that.

I agree. Here's the link:
subject: IOException vs FileNotFoundException
It's not a secret anymore!