Will catch ( EOFException eof )
catch an IOException
that is not an EOFException
?
No.
Since EOFException
is a subclass of IOException
,
the catch block will not catch the latter.
But readInt()
may throw an IOException
.
Say that we wish to catch it.
Here is an attempt to finish the loop:
// Loop with problems try { while ( true ) sum += instr.readInt(); } catch ( EOFException eof ) { System.out.println( "The sum is: " + sum ); instr.close(); } catch ( IOException eof ) { 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
documentation
).