Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7173507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:50:18+00:00 2026-05-28T15:50:18+00:00

With some frequency I find myself writing APIs that offer an Iterator<Foo> which is

  • 0

With some frequency I find myself writing APIs that offer an Iterator<Foo> which is backed by a network connection. The implementation opens a network connection, reads information off the stream, and deserializes that information into Foos to pass on to the caller. Unfortunately there is always the possibility of an IOException and also the need to close the network connection gracefully (that can be made automatic when the caller reads the last Foo, but what if that doesn’t happen?).

There are already a couple of questions (here and here) about how to deal with checked exceptions that would be thrown in an implementation of Iterator, and the accepted advice is “wrap them in unchecked RuntimeExceptions”. Meanwhile to allow closing the network connection we can implement Closeable. So we end up with something like this for a well-behaved exception-checking caller:

Iterator<Foo> iter = null;
try {
    iter = getFooIterator();
    while(iter.hasNext()) {
        Foo foo = iter.next();
        // do something with foo
    }
}
catch(RuntimeException e) {
    if(e.getCause() instanceof IOException) {
       // do something with the IOException
    }
    else throw e;
}
finally {
    if(iter instanceof Closeable) try { ((Closeable)iter).close(); } catch(IOException e) {}
}

And it seemed like such a nice idea to implement Iterator. Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:50:20+00:00Added an answer on May 28, 2026 at 3:50 pm

    IMO the first step would be to wrap it up in an implementation- or app-specific exception, eliminating the need for catching generic RuntimeExceptions or checking the root cause.

    I’d consider a specific implementation to avoid the closeable check and to wrap up the IOException.

    NetworkIterator<Foo> iter = null;
    try {
        iter = getFooIterator();
        while (iter.hasNext()) {
            Foo foo = iter.next();
            // do something with foo
        }
    } catch (NetworkIteratorExceptiom e) {
        // do something with the IOException
    } finally {
        iter.close();
    }
    

    I probably wouldn’t give it method to make the boilerplate go away, but I’d be tempted; roughly:

    NetworkIterator<Foo> iter = new IteratorThang<Foo>() {
        @Override public void each(Foo foo) {
            // Do something with foo
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I frequently find myself writing utility classes that can be re-used throughout my projects.
I am writing a wrapper for plot that automates some tasks that I find
I frequently find myself writing code like this: List<int> list = new List<int> {
I find myself frequently writing code like this: k = 0 for i in
I frequently find myself doing some analysis in R and then wanting to make
I frequently encounter some definitions for Win32API structures (but not limited to it) that
I frequently have some code that should be run either on a schedule or
A problem that has frequently come up in my career is I have some
I'm trying to set some conditional statements for nested embedRelation() instances, and can't find
As part of my current role, I frequently find myself having to work with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.