CSC 102 Lecture Notes Week 5
Shallowness and Deepness
Exceptions
File I/O



  1. Relevant reading.

    1. Horstmann Chapter 10, Special Topic 10.6; Chapter 11

    2. References cited in lab and program writeups


  2. Shallow and deep, equality and copying

    1. In lecture week 2, we discussed the idea of shallow versus deep equality.

    2. This idea extends to copying objects as well.

    3. The Week 5 lecture examples include the following files that illustrate the differences between shallow and deep implementations of important methods:

      1. ShallowCircle.java -- the perils of shallowness

      2. DeepCircle.java -- the benefits of deepness

      3. CircleTester.java -- explaining what goes on

    4. Figure 1 is picture of memory after line 23 of CircleTester.java has executed.


      Figure 1: State of memory after line 23 of CircleTester.java.



    5. Here are some key points about this picture, and the subsequent execution of CircleTester.java:

      1. The ShallowCircle constructor copies the reference to its Point parameter; in contrast, the DeepCircle constructor makes a new copy of the Point parameter.

      2. The ShallowCircle.getCenter method returns a reference to its Point data field; in contrast, DeepCircle.getCenter returns a copy of its Point data field.

      3. The ShallowCircle.equals method compares Point references; in contrast, DeepCircle.equals does a deep comparison by calling Point.equals.


  3. Labs 9 and 10 -- see the writeups

    1. The Lab 9 topic is exception handling

    2. The Lab 10 topic is file I/O


  4. Exceptions (Sections 11.2, 11.3)

    1. You've used throw already.

    2. For lab 9, and beyond, you'll use try-catch to handle exceptions.

    3. You'll also write some exception classes of your own.


  5. The basic idea of exception handling.

    1. Normally, a method returns to its caller.

    2. Exceptionally, a method can throw an exception.

    3. The exception must be caught in order for the program to continue normally.

    4. If a program doesn't catch the exception, Java's runtime will, and your program will then be terminated.

    5. To handle an exception, and avoid program termination, you use try- catch.


  6. The syntax of Java's try-catch

    try {

    ...
    code that may throw
    }
    catch (Exception variable) {

    ...
    code to handle exception
    }


  7. The Throwable hierarchy (Section 11.3)

    1. Exceptions in Java must be defined within the IThrowable class hierarchy.

    2. Here's an excerpt:
      Throwable
          Error
              IOError
              VirtualMachineError
                  ...
          Exception
              IOException
              RuntimeException
                  IndexOutOfBoundsException
                  NullPointerException
                      ...
                  ...
      

    3. There's a very nice picture of this on page 482 of Chapter 11 of the book.


  8. Checked and Unchecked Exceptions (11.4)

    1. The Java compiler requires handling of checked exceptions.

    2. Unchecked exceptions can be ignored.

    3. The classes Error and Runtime exception are unchecked, along with their subclasses.

    4. In the words of Java's authors:

      1. Class Error represents
        "serious problems that a reasonable application should not try to catch"

      2. Class Exception represents
        "a form of Throwable that indicates conditions that a reasonable application might want to catch"

      3. Class RuntimeException represents
        "exceptions that can be thrown during the normal operation of the Java Virtual Machine"


  9. Examples of exceptions

    1. A dumb one, a la lab 8 -- DumbPrint.java

    2. An uncaught banking exception -- BankAccountExceptionTester.java

    3. A caught banking exception -- BankAccountCaughtExceptionTester.java

    4. A caught banking exception, with finally -- BankAccountCaughtFinallyExceptionTester.java

    5. A programmer-defined banking exception -- NegativeBalanceException.java


  10. Throws clause in methods (11.5)

    1. A method that throws a checked exception must declare this with throws in its method header.

    2. This allows the compiler to know that the exception is thrown.


  11. Programmer-defined exceptions (11.7)

    1. Somewhere in the BankAccount program:
      if (amount > balance) {
        throw new
          InsufficientFundsException(...);
      }
      

    2. Define the exception extension:
      public class InsufficientFundsExcpetion
          extends IllegalArgumentException {
      
        public InsufficientFundsException(
              String message) {
          super(message);
        }
      }
      


  12. File I/O (Sections 11.1 and 11.2)

    1. We've seen already how to read from and write to the standard I/O stream that's the UNIX terminal.

    2. You can also read from and write to files.

    3. With a bit of care, doing this is pretty easy.

    4. Here are the highlights:

      1. Use a Scanner to read from a file, calling its constructor like this:
        Scanner s = new Scanner(new File(filename))
        where filename is a String.

      2. Note well the use of new File (...) instead of just the name of the file as a string.

      3. Once you have the Scanner open on the file, you can use it just like you've done in previous labs.

      4. Use a PrintStream to output to a file, calling its constructor like this:
        PrintStream ps = new PrintStream(filename)
        
        where filename is a String

      5. Note that you don't call new File(...) around the filename.

      6. There are a variety of print methods available from PrintStream; see its library documentation for details.

      7. When you're done with the output, be sure to call the PrintStream.close() method, or else your output will not be properly concluded.




index | info | lectures | labs | programs | solutions | examples