Chapter 11 
Review Exercise Solutions
R11.1
If you open a file for reading and the file doesn't exist, then an exception is thrown.
If you open a file for writing and the file doesn't exist, then the file is created with 0 length.
R11.2
If you try to open a read-only file for writing, an exception is thrown.
R11.3
You have to type two backslashes in the quoted string, for example:
FileInputStream in = new FileInputStream("c:\\temp\\output.dat");
R11.4
The command line is the string that a user types into a console window to start a program. For example, if a program is started with the command
java MyClass alpha bravo charlie
then the command line arguments are alpha, bravo, and charlie. These strings are passed to the main method in the args parameter, as an array of strings.
R11.5
ls -f /export/home/user (or dir c:\ /D for Windows)
javac Example.java
R11.6
args[0] = -Dname=piglet
args[1] = -I\eeyore
args[2] = -v
args[3] = heff.txt
args[4] = a.txt
args[4] = lump.txt
R11.7
You throw an exception to notify some other part of the program that an exceptional condition has occurred.
You catch an exception to handle an exceptional situation that has occurred elsewhere in the program.
R11.8
A checked exception is one that the compiler checks when compiling your program. A method that calls another method that threatens to throw a checked exception must either catch that exception or contain a throws clause.
An unchecked exception is not checked at compile time.
The NullPointerException is unchecked.
You need to declare uncaught checked exceptions with the throws keyword.
R11.9
A NullPointerException is an unchecked exception. The programmer is to be blamed when such an exception occurs because it is the responsibility of the programmer to test all references for null before using them instead of installing a handler for that exception.
R11.10
The statement that is executed next is the first statement in the innermost enclosing catch clause that matches the exception type.
R11.11
If an exception has no matching catch clause in the same method, the search for a matching catch clause continues in the calling method. If the entire program has no matching catch clause, then the program terminates.
R11.12
The catch clause can analyze the exception thrown to find out more details about the failure, or it can print an error message to the user that a problem has occurred.
R11.13
No, the type of the thrown exception may be a subtype of the type of the caught exception. That commonly happens, for example, when throwing an EOFException and catching an IOException.
R11.14
You can only throw objects of the Throwable class and it subclasses. You can design your own exception types by extending the subclasses Exception or RuntimeException.
R11.15
You use the finally clause to make sure that certain instructions are executed whether or not an exception is thrown.
A typical example is closing a file.
try
{
   . . .
}
finally
{
   in.close();
}
R11.16
The exception in the finally clause is thrown and may be caught in an enclosing catch clause. The original exception is lost.
For example, the following program prints
 "Caught exception: java.io.IOException: Second Exception"
import java.io.IOException;

public class ExR11_16
{
   public static void main(String args[])
   {
      try
      {
         try
         {
            throw new RuntimeException("First Exception");
         }
         finally
         {
            throw new IOException("Second Exception");
         }
      }
      catch(Exception e)
      {
         System.out.println("Caught exception: " + e);
      }
   }
}
R11.17
The next method can throw:
    NoSuchElementException - if no more tokens are available
    IllegalStateException - if the scanner is closed
The nextInt method can throw:
    InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
    NoSuchElementException - if input is exhausted
    IllegalStateException - if the scanner is closed
They are all unchecked exceptions. The designers of the Scanner class made this choice to make the class easy to use for beginning programmers.
R11.18
If the PrintWriter construction is left outside the try block, the compiler complains because the FileNotFoundException must be caught. The compiler also complains that the IOException is never thrown in the body of the corresponding try statements. 
If we move the construction to inside the try block, then the FileNotFoundException exception would be caught by the IOException catch block. The modified code does not work correctly if the close method throws an exception, because it would not be caught by the catch block. The compiler also complains that the out variable is not seen in the scope of the finally block.
R11.19
We get the error message:

Bad data: End of file expected

Instead, we could print a message indicating that the program expected the file to have numberOfValues data values (in this case, 0), and that more values than expected were found.
R11.20
Yes it can. If filename is null, then the File constructor would throw a NullPointerException.