Closed GoogleCodeExporter closed 9 years ago
To know whether you have a next element you'd need to read the line in
hasNext(), but hasNext() should never throw an exception. Wrapping the
IOException in an unchecked exception seems like a bad idea. (Although, if you
did, it could set as the 'cause' and accessed via getCause(), not a new special
getException() method.)
I think one of two concessions would have to be made:
- Have a new sub-interface of Iterator that can throw a checked exception on
hasNext().
- Or, have a static method somewhere that returns an Iterable<String> from a
Readable. That method throws IOException.
Original comment by ray.j.gr...@gmail.com
on 26 Aug 2010 at 6:38
And... what was I thinking: you can't have a sub-interface that adds a checked
Exception to a method.
So, number 2 is the winner. I realize this blows your idea of being able to
stop iteration prior to the entire file being read in.
Or: and perhaps this is what you were suggesting all along: LineReader is an
Iterator, if an IOException occurs then hasNext() returns false. At the end of
iteration you must call a special getException() method on your LineReader to
see if there was an error. I think that's un-Java-like. Exceptions should be
thrown, not manually checked for.
Original comment by ray.j.gr...@gmail.com
on 26 Aug 2010 at 6:59
>Or: and perhaps this is what you were suggesting all along: LineReader is an
Iterator, if an IOException occurs then hasNext() returns false.
It was. I'd forgotten that hasNext() doesn't throw an exception.
>I think that's un-Java-like. Exceptions should be thrown, not manually checked
for.
You're probably right. I'd thought there was some advantage to not having to
read the entire file as a list (which guava has a method to do already), but
the user would always have to take steps to determine why hasNext() returned
false: because the iteration ended without error, or something bad happened.
Thanks for thinking about this.
Original comment by edbe...@gmail.com
on 28 Aug 2010 at 11:57
Thanks for the suggestion and discussion, guys. Iterators that might throw
exceptions, and are backed by a stream that will need to be closed, are highly
problematic, and we don't have a good solution for this.
Original comment by kevinb@google.com
on 8 Sep 2010 at 6:06
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:15
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:09
Original issue reported on code.google.com by
edbe...@gmail.com
on 24 Aug 2010 at 4:21