go to previous page   go to home page   go to next page

Will catch ( EOFException eof ) catch an IOException that is not an EOFException?

Answer:

No.


Another Exception

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
public static void main()
{
  . . .
   
  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.


QUESTION 10:

Does DataInputStream.close() throw an exception? (Look at the documentation ).