Friday, September 2, 2016

Did You Know: It is optional to declare a throws clause in Java

I ran into an interesting aspect of Java the other day that I was heretofore unaware of. I asked my very first question on Stack Overflow about it as I was surprised I was able to Remove a Java checked exception while overriding an interface method.

Turns out the answer as to why this is allowed is quite simple. Take java.lang.Appendable as the example which defines three methods, all of which throws IOException. Since I had overridden each of these methods in MyInterface, I can remove throws IOException from each method in MyInterface because a MyInterfaceImpl will then never throw an IOException. Duh!

Here is what the calling code looks like in this newly discovered scenario:

 MyInterface mine = new MyInterfaceImpl();
 Appendable a = mine;

 // No need to try-catch for IOException when using MyInterface
 mine.append("Foo");

 // However, still need to try-catch when using Appendable
 try {
    a.append("Bar");
 } catch (IOExcpetion e) {}

Apparently, it is also optional to throw checked exceptions while directly implementing interfaces. This doesn't seem quite as useful, though, unless the calling code always references the concrete class.

I'm now thinking of where I might be able to put this golden nugget to good use (java.sql package anyone?).

No comments:

Post a Comment