Problems that arise in you code during execution

public void readFile(String filename)
throws FileNotFoundException
{
...
}
public static void main(String[] args)
{
try
{
readFile("input.txt");
...
}
catch (FileNotFoundException e)
System.out.println("Error: file not found: "
+ "input.txt");
You can also catch Runtime exceptions
if statement will worktry { 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"); }
Suppose the process method throws a EOFException. What
gets
printed?
try
{
process(filename)
}
catch (FileNotFoundException ex)
{
System.out.println("File not found"); //a
}
catch (IOException ex)
{
System.out.println("I/O error"); //b
}
catch (RuntimeException ex)
{
System.out.println("Something went wrong"); //c
}
finally Clausereader = new FileReader(filename); Scanner in = new Scanner(reader); readData(in); reader.close(); // May never get here
reader.close() even if exception happens finally clause for code that must be executed no matter what
finally ClauseScanner in = new Scanner(reader);
try
{
readData(in);
}
finally
{
in.close();
}
try block is exited in any of three ways:
try block catch clause, if this try
block caught an exception try block and not caught
try (Scanner in = new Scanner(file))
{
readData(in);
} // in.close called automatically—Scanner is AutoCloseable
public void read(String filename)
{
Scanner in = null; //why asign a null value?
try
{
in = new Scanner(new File(filename));
process(in);
}
catch (IOException ex) { System.out.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 exceptionProcess of exception handling is a search for a competent handler