Wednesday, November 19, 2014

ByteArrayOutputStream Checked Exceptions

Just realised...

ByteArrayOutputStream actually doesn't declare 'throws IOException' on the methods it does override. I thought it did, because the method I use all the time, write(byte[]), does declare throws IOException, just to annoy us.

What it could do, but doesn't do, is that it could override the other methods so that you don't have to be annoyed by their declaring throws IOException, even though it will, of course,  never throw them.

Checked exceptions...yeah, that experiment turned out great.


Update: Here's a variant of FileInputStream which avoids checked exception in try-with-resources; which you could instantiate inside of a utility method, say 'public static FileInputStreamNoce istreamForFile(File srcFile)' wherein you catch FileNotFoundException.

 public static class FileInputStreamNoce extends FileInputStream {
  public FileInputStreamNoce(File file) throws FileNotFoundException {
   * This avoids having to catch checked exception in try-with-resources.
  public void close() {
   try {
   } catch (IOException e) {
    // well, I don't know what to do here. Log? Re-throw wrapped inside RTE?

No comments:

Post a Comment