Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

208Throwing Exceptions from Fields

Author: Dr. Heinz M. KabutzDate: 2013-03-31Java Version: 1.4Category: Language
 

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.

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...