Let’s assume we have a nice abstraction over email messages simply called Email
. Something that can write itself to Writer
and provide some other functionality (e.g. operating on the inbox).
Now, take this piece of code:
public void saveEmail(Email email) { FileOutputStream fos = null; try { fos = new FileOutputStream("file.txt"); PrintWriter pw = new PrintWriter(fos); email.writeContentTo(pw); pw.flush(); email.deleteMessageFromInbox(); } catch (IOException e) { log.error("Saving email", e); } finally { IOUtils.closeQuietly(fos); } }
Question: What happens if for whatever reason the PrintWriter
cannot write to disk? Say, your drive just filled up and you can’t squeeze a byte in there?
To my surprise, nothing got logged, and the messages were removed from inbox.
Unfortunately, I learned it the hard way. This code is only a piece of a larger subsystem and it took me quite a while to figure out. Used to dealing mostly with streams, I skipped this area and checked everything else first. If PrintWriter
was like a stream, it would throw an exception, skipping deleteMessageFromInbox
.
All streams around files work like that, why would this one be different? Well, PrintWriter
is completely different. Eventually I checked its Javadoc and saw this:
“Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().”
At first I was furious about finding that’s all due to yet another inconsistency in core Java. Now I’m only puzzled and a bit angry. Most things in Java work like this – they throw exceptions on every occasion and force you to explicitly deal with them. And it’s not unusual to see 3 or 4 of them in a signature.
One could say, it’s all right there in the Javadoc. Correct. But still, it can be confusing to have two fundamentally different approaches to error handling in one corner of the library. If one gets used to dealing with one side of it, he may expect the same from the other. Instead, it turns out you always have to carefully look at signatures and docs, because even in core Java there is no single convention.
Related: try/catch/throw is an antipattern – interesting read, even if a bit too zealous.
You’re using the wrong class. To bridge from Writer to OutputStream, you use an OutputStreamWriter *with*the*correct*encoding*specified*, not a PrintStream.