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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:58:53+00:00 2026-06-14T07:58:53+00:00

A few days ago I asked a question about reading from file until the

  • 0

A few days ago I asked a question about reading from file until the input has finished. I’ve received some responses which I studied, but I have one question somehow relevant to the topic: why do I have to use some additional tests, “finally” clause and another try-catch there instead of just:

DataInputStream reading = null;
try{
        reading = new DataInputStream(new FileInputStream("tes.t"));
        while(true)
        {
            System.out.println(reading.readInt());
            System.out.println(reading.readDouble());
        }
        }catch(IOException xxx){System.err.println("Error: " + reading.getMessage());}
         catch(EOFException xxx){reading.close();}

Why is this not enough? I mean – as soon as we reach EOF, we’ll have an exception. Otherwise, we catch the exception and print the error message. Why do we need additional tests? What’s wrong with this approach?

EDIT: OK, I now understand why it’s a bad thing. But what about if I made the code like this:

DataInputStream reading = null;
try{
        reading = new DataInputStream(new FileInputStream("tes.t"));
        while(reading.available()!=0)
        {
            System.out.println(reading.readInt());
            System.out.println(reading.readDouble());
        }
        }catch(EOFException xxx){}
         catch(IOException xxx){System.err.println("Error: " + reading.getMessage());}

Is checking the value of reading.available() a better test for the input end or just as bad?

  • 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-06-14T07:58:54+00:00Added an answer on June 14, 2026 at 7:58 am

    It’s not elegant to control your execution flow with exceptions. EOF is a normal situation while reading a file so it should be handled in other way.

    The more elegant code would be:

    byte[] buf = new byte[8192];
    int nread;
    while ((nread = is.read(buf)) >= 0) {
      // process the first `nread` bytes of `buf`
    }
    

    source: Unknown buffer size to be read from a DataInputStream in java

    Here is you can find very good explaintation why you shouldn’t control flow with exceptions: Why not use exceptions as regular flow of control?

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

Sidebar

Related Questions

A few days ago I has asked a question on SO about helping me
A few days ago I asked a question about returning select fields from a
A few days ago I asked this question about jquery ajax function invoking action
I asked a question a few days ago about how to customise the calendar
A few days ago I asked about passing a data structure from java to
A few days ago i asked this question: Displaying images from Facebook photo albums
Few days ago I have asked a question about 1,2 and 3. degree connections.
A few days ago I asked a very similar question. I was about returning
I asked a question about the undefined value in javascript a few days ago.
Well, I asked a question about sorting few days ago. I found out how

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.