To be pedantic, you do not implement Iterator.next if you need to add a checked exception. Iterator.next does not throw excepted exceptions, so basically Iterator.next has a contract that nothing bad can go the way the user really should think (against, say, File.open, where you need to worry about what IOExceptions will get throw).
Given this, I would consider the following options in the following order:
Can I remove checked exceptions and replace them with unchecked exceptions? This would be my first attempt, because the checked exceptions cause some annoyance every time they appear. This will easily resolve your solution, because then your next () will no longer throw checked exceptions. If this is a third-party library, can I wrap it (which would be a good idea, anyway) and turn these exceptions into excluded exceptions in the shell?
Can I wrap exceptions from runtime exceptions in an iterator? There are constructors for this (those who have Throwable). This at least encapsulates what you use internally and allows you to use the Iterator interface
Can I replace Iterator with Iterable? If so, I can throw these checked exceptions during Iterable creation, and then the iterator cannot throw exceptions. This will only work if the number of elements is not large.
Tetha source share