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

Trail: Essential Java Classes
Lesson: Handling Errors with Exceptions

Questions and Exercises: Exceptions

Questions

  1. Is the following code legal?
    try {
        ...
    } finally {
        ...
    }
    
  2. What exception types can be caught by the following handler?
    catch (Exception e) {
        ...
    }
    
    What is wrong with using this type of exception handler?

  3. What exceptions can be caught by the following handler?
    ...
    } catch (Exception e) {
        ...
    } catch (ArithmeticException a) {
        ...
    }
    
    Is there anything wrong with this exception handler as written? Will this code compile?

  4. Match each situation in the first column with an item in the second column.
    1. int[] A;
      A[0] = 0;

    2. The Java VM starts running your program, but the VM 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.
    1. error

    2. checked exception

    3. runtime exception

    4. no exception

Exercises

  1. 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.

  2. 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();
            }
        }
    }
    
Check your answers.(in the Essential Java Classes trail)

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

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