CS51 - Fall 2009 - Lecture 33

  • Exercise 18.5.1

  • look at StupidName code
       - try it without try-catch block
       - compiler complains: "Unhandled exception type IOException"

  • checked vs. unchecked exceptions

  • we have two options for dealing with checked exceptions
       - we can put a try catch block around the code
       - or, we can say that we don't want to deal with it here and announce to the word that our method throws that exception
       - we do this by appending: "throws <exception_class_name>" after the method description before the '{'

  • throwing exceptions
       - two reasons why a method is required to announce that is throws a particular exception
          - a method it calls throws an exception and the method doesn't handle it
          - the method explicitly throws an exception
       - throw new <exception_class_name>()
          - note it's just a constructor for a class
             <exception_class_name> exceptionToThrow = new <exception_class_name>();
             throw exceptionToThrow;
          - throw is similar to return... exits the current code

  • show StupidNameWithBadName demo
       - how can we do this?
          - We could modify readName(), but what should we return if there is a problem?
          - We could modify our begin method to check if the name is good, but then we'd have to check that every time we called the readName method
          - Use an exception
             - We can define our own exceptions by extending Exception
             - look at BadNameException
                - usually end the class name with "Exception"
                - override getMessage() method
                - could in theory extend RuntimeException, in which case it would be an unchecked exception. In general, good coding practice to make all you exceptions checked.

  • Why do we need files?
       - persist data

  • Basic computer hardware
       - processor
       - main memory
       - hard-drive

  • show SantasListBasic demo
       - similar to BadName
       - now we don't have to enter the names
       - we can read them from a file

  • What operations might you do on a file?
       - open
       - read
       - write
       - close/save

  • Reading data from files
       - Parent class is "Reader"
       - Two main Readers
          - FileReader(String filename)
             - read() // reads one character at a time
             - is of type "Reader"
          - BufferedReader(Reader reader)
             - readLine();
                - returns the next whole line as a String
                - if no more data is available (end of file) returns null
             - notice it takes as input another reader, why?
                - we'll see later, can be used for more than just reading files
                - could pass in another type of reader
             - is also of type "Reader"
          - Both of these classes are in java.io
             - we need to import these classes at the top, like we do with objectdraw:
                import java.io.*;

  • look at names file

  • BufferedReader reader = new BufferedReader(new FileReader(String filename))
       - how can we read all of the data from the names file using a BufferedReader?

  • look at SantasListBasic code
       - try-catch IOException
          - when creating readers (i.e. opening the file), why?
             - file could not be there
             - file could not be readable (i.e. you don't have access to read it)
          - when reading data from a file
             - device failure (i.e. hard-drive fails or has problems)
             - corrupted file
       - Common setup when reading data
          //read one line
          while( line != null ){
             // do something with the line
             // read another line
       - import java.io.*;