Today I was working with Java class StringReader
and I found it very annoying that it throws IOException on read
method. I know that it extends Reader
class in which method read
throw IOException but I think that it is not needed for StringReader. This class doesn't use any external resources that might cause errors.
After short investigation I found out that StringReader#read
throws IOException
if string which this class reads is null but de facto this can't happen because if we would try to pass null to StringReader
constructor it throws NPE.
What do you think about it, is it good practice to always throw the same exceptions as super class?
Edit: As noted by U Mad Reader is a class not interface.
StringReader
is mandatory, because the interfaceReader
requires it even if the implementing class won't never throws such an exception. – Monarchyread
(and other methods) without the exception then the following would hold true:Reader r = new StringReader; r.read();
would require exception handling.StringReader r = new StringReader;r.read();
would not require exception handling. – Iveyivie