站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Answers to Questions and Exercises - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Handling Errors Using Exceptions

Answers to Questions and Exercises

Questions

  1. Question: Is the following code legal?
    try {
        
    } finally {
       
    }
    
    Answer: Yes, it's legal. A try statement does not have to have a catch statement if it has a finally statement. If the code in the try statement has multiple exit points and no associated catch clauses, the code in the finally statement is executed no matter how the try block is exited.

  2. Question: What exception types can be caught by the following handler?
    catch (Exception e) {
         
    }
    
    What is wrong with using this type of exception handler?

    Answer: This handler catches exceptions of type Exception; therefore, it catches any exception. This can be a poor implementation because you are losing valuable information about the type of exception being thrown and making your code less efficient. As a result, your program may be forced to determine the type of exception before it can decide on the best recovery strategy.

  3. Question: Is there anything wrong with this exception handler as written? Will this code compile?
    try {
    
    } catch (Exception e) {
       
    } catch (ArithmeticException a) {
        
    }
    
    Answer: This first handler catches exceptions of type Exception; therefore, it catches any exception, including ArithmeticException. The second handler could never be reached. This code will not compile.

  4. Question: Match each situation in the first list with an item in the second list.
    1. int[] A;
      A[0] = 0;
    2. The JVM starts running your program, but the JVM can't find the Java platform classes. (The Java platform classes reside in classes.zip or rt.jar.)
    3. A program is reading a stream and reaches the end of stream marker.
    4. Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again.

    __error

    __checked exception

    __compile error

    __no exception

    Answer:

    1. 3 (compile error). The array is not initialized and will not compile.
    2. 1 (error).
    3. 4 (no exception). When you read a stream, you expect there to be an end of stream marker. You should use exceptions to catch unexpected behavior in your program.
    4. 2 (checked exception).

Exercises

  1. Exercise: Add a readList method to ListOfNumbers.java (in a .java source file). This method should read in int values from a file, print each value, and append them to the end of the vector. You should catch all appropriate errors. You will also need a text file containing numbers to read in.

    Answer: See ListOfNumbers2.java (in a .java source file).

  2. Exercise: Modify the following cat method so that it will compile:
    public static void cat(File named) {
        RandomAccessFile input = null;
        String line = null;
    
        try {
            input = new RandomAccessFile(named, "r");
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            return;
        } finally {
            if (input != null) {
                input.close();
            }
        }
    }
    

    Answer: The code to catch exceptions is shown in red:

    public static void cat(File named) {
        RandomAccessFile input = null;
        String line = null;
    
        try {
            input = new RandomAccessFile(named, "r");
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            return;
        } catch(FileNotFoundException fnf) {
            System.err.println("File: " + named + " not found.");
        } catch(Exception e) {
            System.err.println(e.toString());
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch(IOException io) {
                }
            }
        }
    }
    


Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.