Another Exception
Since EOFException is a subclass of IOException the
catch block will not catch the latter.
But readInt() may throw an IOException and it must
be caught somewhere.
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.
|