Wednesday, October 17, 2007

New IOException Constructors in JDK 6

In JDK 1.5 or earlier, IOException only has 2 constructors:
IOException() and IOException(String s).

So you can't pass a cause exception or throwable to these constructors. To do that, you will need to do something like this:

ZipException ze = new ZipException("Nested ZipException");
IOException e = new IOException("IOException with nested ZipException");
e.initCause(ze);
throw e;




In JDK 6, 2 additional constructors are added:

IOException(String s, Throwable cause)
IOException(Throwable cause)

So the following can compile and run fine in JDK 6 and later:


ZipException ze = new ZipException("Nested ZipException");
IOException e = new IOException(ze);
throw e;

but will fail to compile in JDK 1.5 or earlier:

c:\tmp > javac -version A.java
javac 1.5.0_06A.java:15: cannot find symbol
symbol : constructor IOException(java.util.zip.ZipException)
location: class java.io.IOException
IOException e = new IOException(ze);
^1 error

No comments:

Post a Comment