EOFException - how to handle?

后端 未结 7 1098
迷失自我
迷失自我 2020-11-27 17:11

I\'m a beginner java programmer following the java tutorials.

I am using a simple Java Program from the Java tutorials\'s Data Streams Page, and at runtime, it keeps

相关标签:
7条回答
  • 2020-11-27 17:34

    Alternatively, you could write out the number of elements first (as a header) using:

    out.writeInt(prices.length);
    

    When you read the file, you first read the header (element count):

    int elementCount = in.readInt();
    
    for (int i = 0; i < elementCount; i++) {
         // read elements
    }
    
    0 讨论(0)
  • 2020-11-27 17:39

    You may come across code that reads from an InputStream and uses the snippet while(in.available()>0) to check for the end of the stream, rather than checking for an EOFException (end of the file).

    The problem with this technique, and the Javadoc does echo this, is that it only tells you the number of blocks that can be read without blocking the next caller. In other words, it can return 0 even if there are more bytes to be read. Therefore, the InputStream available() method should never be used to check for the end of the stream.

    You must use while (true) and

    catch(EOFException e) {
    //This isn't problem
    } catch (Other e) {
    //This is problem
    }
    
    0 讨论(0)
  • 2020-11-27 17:39

    Put your code inside the try catch block: i.e :

    try{
      if(in.available()!=0){
        // ------
      }
    }catch(EOFException eof){
      //
    }catch(Exception e){
      //
    }
    }
    
    0 讨论(0)
  • 2020-11-27 17:44

    You catch IOException which also catches EOFException, because it is inherited. If you look at the example from the tutorial they underlined that you should catch EOFException - and this is what they do. To solve you problem catch EOFException before IOException:

    try
    {
        //...
    }
    catch(EOFException e) {
        //eof - no error in this case
    }
    catch(IOException e) {
        //something went wrong
        e.printStackTrace(); 
    }
    

    Beside that I don't like data flow control using exceptions - it is not the intended use of exceptions and thus (in my opinion) really bad style.

    0 讨论(0)
  • 2020-11-27 17:45

    The best way to handle this would be to terminate your infinite loop with a proper condition.

    But since you asked for the exception handling:

    Try to use two catches. Your EOFException is expected, so there seems to be no problem when it occures. Any other exception should be handled.

    ...
    } catch (EOFException e) {
       // ... this is fine
    } catch(IOException e) {
        // handle exception which is not expected
        e.printStackTrace(); 
    }
    
    0 讨论(0)
  • 2020-11-27 17:54

    While reading from the file, your are not terminating your loop. So its read all the values and correctly throws EOFException on the next iteration of the read at line below:

     price = in.readDouble();
    

    If you read the documentation, it says:

    Throws:

    EOFException - if this input stream reaches the end before reading eight bytes.

    IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

    Put a proper termination condition in your while loop to resolve the issue e.g. below:

         while(in.available() > 0)  <--- if there are still bytes to read
    
    0 讨论(0)
提交回复
热议问题