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 109469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:04:27+00:00 2026-05-11T02:04:27+00:00

In java a class can implement Iterable which lets you use the foreach() statement

  • 0

In java a class can implement Iterable which lets you use the foreach() statement and the iteration syntatic sugar:

for(T t:ts) ... 

However, this does not allow you to throw exceptions on the construction for an Iterator. If you were iterating off a network, file, database etc it would be nice to be able to throw exceptions. Obvious candidates are java.io.InputStream, Reader and the java.nio.Channel code, but none of this can use Generics like the Iterable interface can.

Is there a common idiom or Java API for this situation?

Clarification: This is asking if there is a pattern or alternative interface for iterating for objects off a non-memory source. As responders have said, just throwing RuntimeExceptions to get around the problem is not recommended or what I was looking for.

Edit 2: Thanks to answers so far. The consensus seems to be ‘you can’t’. So can I extend the question to ‘What do you do in this situation, when this situation would be useful?’ Just write your own interface?

  • 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. 2026-05-11T02:04:28+00:00Added an answer on May 11, 2026 at 2:04 am

    Unfortunately you can’t. There are two problems:

    • The Iterator API doesn’t declare any exceptions to be thrown, so you’d have to throw RuntimeExceptions (or non-Exception throwables)
    • The enhanced for loop doesn’t do anything to try to release resources at the end of the loop

    This is very annoying. In C#, for instance, you can really easily write code to iterate through the lines of a text file:

    public static IEnumerable<string> ReadLines(string filename) {     using (TextReader reader = File.OpenText(filename))     {         string line;         while ( (line=reader.ReadLine()) != null)         {             yield return line;         }     } } 

    Use as:

    foreach (string line in ReadLines('foo.txt')) 

    The foreach loop calls Dispose on the IEnumerator in a finally block, which translates to ‘check if we need to do anything in the iterator block’s finally (from the using statement)’. Obviously there are no checked exceptions in C#, so that side of things isn’t a problem either.

    A whole (useful!) idiom is pretty much unworkable in Java due to this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 60k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Use them where you need to use/manipulate patterns. For instance,… May 11, 2026 at 9:33 am
  • added an answer Since you've marked it language-agnostic, here's the algorithm I'd use.… May 11, 2026 at 9:32 am
  • added an answer First you should make your exceptions visible by changing the… May 11, 2026 at 9:32 am

Related Questions

In java a class can implement Iterable which lets you use the foreach() statement
In Java, I use a class in which some fields can be null .
In Java, can Class.forName ever return null, or will it always throw a ClassNotFoundException
What is a synthetic class in Java? Why should it be used? How can
I want to implement in Java a class for handling graph data structures. I
In Java (And in general) is there a way to make a class so
In Java, say you have a class that wraps an ArrayList (or any collection)
What characters are valid in a Java class name? What other rules govern Java
I once wrote this line in a Java class. This compiled fine in Eclipse
I was writing a toString() for a class in Java the other day by

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.