I just noticed that ByteArrayOutputStream.write does not actually throw an IOException, but Eclipse complains about an unhandled exception when I use it ... weird.
This is easy to explain. You probably did something like this:
OutputStream os = new ByteArrayOutputStream(); ... os.write();
The "problem" is that you are calling the method as OutputStream.write() , and not as ByteArrayOutputStream.write() . Therefore, the compiler says:
"Ah ... write() on an OutputStream may throw an IOException , so you need to deal with it."
You can not say:
"This particular OutputStream really is a ByteArrayOutputStream ... so we'll let you go."
because JLS does not allow it.
This is one of those extreme cases when, after transcoding to the interface, and not to the implementation class, the next "best practice" returns to bite you.
OK, therefore ...
- its soft gap, not a complete bite.
OutputStream is implemented as a Java class, not a Java interface, but this is not relevant.- most compilers don't actually talk to you when compiling your code :-)
Stephen c
source share