Results 1 to 4 of 4
  1. #1
    SmugMug is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Using exceptions to read to end of file -- bad form?

    There's a second issue I ran into while learning to code in Java. I'm having to teach myself, so I don't have the luxury of having anyone to ask for insight.

    I wrote a short program which reads an arbitrary number of objects from a file into a vector. The program doesn't know how many of these objects there are, and I had trouble googling up a proper way of handling pulling an arbitrary number of objects from a file. What I ended up doing was using the EOF exception to make sure I read to the end of the file, like so:

    Java Code:
    try {
    	while (true) {
    		someVector.add(objectIn.readObject());
    	}
    } catch (EOFException eof) {};
    I might be new to java, but I do have a teensy bit of C++ experience from college...enough to give me the feeling that this is bad form. What be a better way of handling this type of situation?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Using exceptions to read to end of file -- bad form?

    You might consider ways of avoiding the exception other than for the case of truly exceptional things. Eg you could serialise the size of the list, then read in that many objects. Or you could serialise/deserialise the list as a whole rather than its contents object by object.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Using exceptions to read to end of file -- bad form?

    Why is it bad? Some classes return null/-1 when the end of the file have been reached, you might prefer that.

  4. #4
    SmugMug is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Using exceptions to read to end of file -- bad form?

    Quote Originally Posted by pbrockway2 View Post
    You might consider ways of avoiding the exception other than for the case of truly exceptional things. Eg you could serialise the size of the list, then read in that many objects. Or you could serialise/deserialise the list as a whole rather than its contents object by object.
    It never so much as crossed my mind to serialize the size of the list. That makes a lot of sense. I'll play around with it and see what happens!

    Quote Originally Posted by Pojahn_M View Post
    Why is it bad? Some classes return null/-1 when the end of the file have been reached, you might prefer that.
    That's true. I'm happy with the solution I came up with because it works, but I have a feeling that more experienced coders would see using exceptions this way to be inelegant. My goal is to code on a professional level. I just want to make sure I don't develop bad habits, especially so early in the learning process.

Similar Threads

  1. Replies: 8
    Last Post: 10-03-2010, 10:45 AM
  2. [SOLVED] Apache Commons Properties file exceptions
    By edwardmluk in forum Java Servlet
    Replies: 1
    Last Post: 03-06-2009, 05:24 PM
  3. RegExp to remove tag from html file with exceptions
    By Daedalus in forum Advanced Java
    Replies: 3
    Last Post: 09-27-2008, 05:43 AM
  4. how to read a form with JFS
    By crispy in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-01-2008, 04:07 PM
  5. Replies: 1
    Last Post: 06-07-2007, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •