Will catch ( EOFException eof )
catch an IOException
that is not an EOFException
?
No.
Since EOFException
is a subclass of IOException
,
the above catch block will not catch the IOException
.
But readInt()
may throw an IOException
.
Say that we wish to catch it.
Here is an attempt to finish the loop:
// Loop with problems public static void main() { . . . try { while ( true ) sum += instr.readInt(); } catch ( EOFException eof ) { System.out.println( "The sum is: " + sum ); instr.close(); } catch ( IOException iox ) { System.out.println( "Problem reading input" ); instr.close(); } . . . }
This is syntactically correct.
It is OK to have several catch{}
blocks with the
most specific exceptions listed first.
But there is a problem.
Does DataInputStream.close()
throw an exception?
(Look at the