EOFException - how to handle?

后端 未结 7 1130
迷失自我
迷失自我 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: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.

提交回复
热议问题