Java Reference
In-Depth Information
file.close();
}
}catch(IOException e)
{}
}
System.out.println(“Just read “ + x);
return x;
}
}
The following FinallyDemo program instantiates a MyFileUtilities3 object
and invokes its readOneByte() method. See if you can determine its output
when no exception occurs. What happens when a FileNotFoundException
occurs? What happens when an IOException occurs?
Figure 11.10 shows the output when the file is not found.
import java.io.FileNotFoundException;
public class FinallyDemo
{
public static void main(String [] args)
{
System.out.println(“Instantiating a MyFileUtilities3
object...”);
MyFileUtilities3 util = new MyFileUtilities3(args[0]);
System.out.println(“Invoking readOneByte() method...”);
try
{
byte b = util.readOneByte();
System.out.println(b);
}catch(FileNotFoundException e)
{
System.out.println(“Could not find “ + args[0]);
e.printStackTrace();
}
System.out.println(“End of main”);
}
}
Figure 11.10
Output of the FinallyDemo program when a FileNotFoundException occurs.
Search WWH ::




Custom Search