Thursday, September 14, 2006

You Know IOException? Think Again!

I was amazed today to find out that there was no constructor IOException(String, Throwable) or IOException(Throwable) in JDK1.4 and JDK1.5. It is finally in JDK1.6, I can't believe it took Sun that much time to change that.

So the workaround is:
IOException ioe = new IOException("message");
ioe.initCause(e);
throw ioe;
It can also be written as:
throw (IOException) new IOException("message").initCause(e);
It is not a major problem, but still. We can all thank the guy who reported that as a bug to Sun in 2004.

1 comment :

  1. the code sure is handy... but i have just one question though; why do you need to throw IOException? Isn't this analogous to throwing Exception, which is an anti-pattern.

    ReplyDelete