opennlp-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jörn Kottmann <kottm...@gmail.com>
Subject Re: Iterator vs. Streams
Date Sun, 30 Jan 2011 18:10:30 GMT
On 1/30/11 11:27 AM, Steven Bethard wrote:
> I think we have a basic disagreement about what is buggy user code and
> what isn't. I believe that declaring a method as "throws" is not buggy
> code, and I believe that using RuntimeExceptions instead of checked
> exceptions is not buggy code as long as they're declared to be thrown
> (e.g. like SecurityException on File.getAbsolutePath). But I don't
> think we're going to come to an agreement on this issue, so let's set
> it aside.

No, I really mean code which has serious issues. It does not really matter
if you use RuntimeException or not, but if you do not close the underlying
resources correctly in case of an exception you might run into a bug
depending on your application.

I still believe that it is confusing to use Iterators in a combination
with RuntimeException and the strict need to close them, but I might
be mistaken.

I am pretty sure with the Iterator interface people tend to do the
following mistakes frequently:
- Assuming no exception can be thrown
- Not understanding that writing a second for-each implies a reset of 
the stream
- Not using try-finally to safely close the Iterators
- Only expecting IOExceptions when they are declared as throws
    (like in your example) but not also a RuntimeIOException

If it is just some code which runs once it does not really matter, sure. But
if it is maybe a small server which processes the ObjectStreams over and
over again, and should not fail even after it is running for quite some 
time,
then it does matter.

We would get the issues above, and the only thing we get for the user
is that it is a little more convenient to use for-language features 
which could also
be done with a while loop.

We also do not keep you in anyway from just doing would you like to do,
making an Iterator which reads from an ObjectStream only takes a few lines.
Maybe you do not want to use checked exceptions in your application, fine,
then I maybe would make a small util class which helps dealing with a 
few cases in
the Java API easily, or maybe there is even a small library out there which
could be used for that.

Jörn

Mime
View raw message