

new Scanner(file) can throw FileNotFoundExceptionthrows to alert the caller
public static void process(String filename) throws FileNotFoundException public static void main(String[] args) throws FileNotFoundException
When you call a[i] on an array, your code might throw an
ArrayIndexOutOfBoundsException. Why don't you have to tag
methods
with throws ArrayIndexOutOfBoundsException?
ArrayIndexOutOfBoundsException is a checked
exceptionArrayIndexOutOfBoundsException is checked at
compile-timei is validNullPointerException
(if a is null)try { String filename = . . .; FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); String input = in.next(); int value = Integer.parseInt(input); . . . } catch (IOException exception) { exception.printStackTrace(); } catch (NumberFormatException exception) { System.out.println("Input was not a number"); }
throws, catch 
try
{
process(filename)
}
catch (FileNotFoundException ex) { println("File not found"); }
catch (IOException ex) { println("I/O error"); }
catch (RuntimeException ex) { println("Something went wrong"); }}
Suppose process throws a EOFException. What
gets
printed?
reader = new FileReader(filename); Scanner in = new Scanner(reader); readData(in); reader.close(); // May never get here
no matter what
Scanner in = new Scanner(file);
try
{
readData(in);
}
finally
{
in.close();
}
try (Scanner in = new Scanner(file))
{
readData(in);
} // in.close called automatically—Scanner is AutoCloseable
public void read(String filename)
{
Scanner in = null;
try
{
in = new Scanner(new File(filename));
process(in);
}
catch (IOException ex) { println("I/O Error"); }
finally { in.close(); }
}
Which of the following is true if the file does not exist?
FileNotFoundException is thrown to the caller of readNullPointerException is thrown to the caller of readread method returns without printing anything and
without an exception