Java Specialists' Java Training Europehome of the java specialists' newsletter

The Java Specialists' Newsletter
Issue 2082013-03-31 Category: Language Java version: Java 1.0 - 8

GitHub Subscribe Free RSS Feed

Throwing Exceptions from Fields

by Dr. Heinz M. Kabutz
Abstract:
How can you set a field at point of declaration if its constructor throws a checked exception?

Welcome to the 208th issue of The Java(tm) Specialists' Newsletter, sent from the beautiful Island of Crete. Whilst my friends in Germany are building snowmen, we are suffering with sunburn whilst making barbecues :-) A wonderful Sunday to you. I won't wish you "Happy Easter", as we won't celebrate that until May here in Greece.

After my recent request to confirm your contact details, we now have three new countries on our list: Bermuda, Montenegro and CuraƧao. One of these countries has been around shorter than this newsletter - I will leave it up to you to figure out which one :-)

We split the world into different regions, to allow us to cater specifically for your part of the world. Time zones do get in the way when we plan webinars, for example. I was quite surprised that the majority of my readers are based in Europe, followed by North America and then India. This means I will be doing most of my conference speaking in Europe.

Also interestingly, of those who responded to my question about their Java skill, 3% are beginners, 27% are intermediate, 63% are advanced and 6% are guru. Based on the emails that I receive every week, I really thought that we had far more Java beginners on our list. What is really exciting about these values is that my job for the last 12 years has been to get programmers from the intermediate stage to the advanced stage. And once there, to get them to the guru skill level.

NEW: We have revised our "Advanced Topics" course, covering Reflection, Java NIO, Data Structures, Memory Management and several other useful topics for Java experts to master. 2 days of extreme fun and learning. Extreme Java - Advanced Topics.

Throwing Exceptions from Fields

In Java, when the constructor of an object that we are storing in a field throws an exception, we usually set it in our class' constructor. For example:

import java.io.*;
public class Foo implements AutoCloseable {
   private final ObjectInputStream in;
   public Foo() throws IOException {
     in = new ObjectInputStream(
       new BufferedInputStream(
         new FileInputStream("data.bin")));
   }
   public void close() throws IOException {
     in.close();
   }
   protected void finalize() throws Throwable {
     close();
     super.finalize();
   }
}

However, did you know that you could also construct the ObjectInputStream inside the field, as long as you had a constructor that threw the IOException? I knew this was possible, but to my shame I had not tried it out. Here is what you could do:

import java.io.*;
public class Foo implements AutoCloseable {
  private final ObjectInputStream in =
    new ObjectInputStream(
      new BufferedInputStream(
        new FileInputStream("data.bin")));
  public Foo() throws IOException {
  }
  public void close() throws IOException {
    in.close();
  }
  protected void finalize() throws Throwable {
    close();
    super.finalize();
  }
}
  

I hope you find this interesting. It is the type of factoid that can easily be turned into an interview question.

Kind regards

Heinz

Language Articles Related Java Course

Extreme Java - Concurrency and Performance for Java 8
Extreme Java - Advanced Topics for Java 8
Design Patterns
In-House Courses

© 2010-2016 Heinz Kabutz - All Rights Reserved Sitemap
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. JavaSpecialists.eu is not connected to Oracle, Inc. and is not sponsored by Oracle, Inc.