Yes.
try{}
Blocks
try
{
// other stuff goes here
try
{
while ( true )
sum += instr.readInt();
}
catch ( EOFException eof )
{
System.out.println( "The sum is: " + sum );
instr.close();
}
catch ( IOException iox )
{
System.out.println( "Problems reading " + fileName );
instr.close();
}
}
catch ( IOException iox )
{
System.out.println( "I/O Problems with" + fileName );
}
A method can catch some exceptions and not others. Since problems with closing a file are rare, not catching this exception would be tolerable. The logic is messy, but I/O programming often is.
This example is probably
more complicated than you would
normally write.
But a commercial-grade program should take
care of everything.
It could be slightly simplified by using a finally{}
block inside the nested try{}
block.
If the readInt()
throws an IOException
,
where will it be caught?