Lecture 36, CS 302-6, November 28

  1. Various things
    1. Program 3

                                                               i.     Due tonight

  1. Review:
    1. Parsing text from files

                                                               i.     Reading word-by-word

1.    Custom delimiters

                                                             ii.     Reading line-by-line

1.    Three ways to parse words out a line of text in a String s:

a.    String and Character methods, such as s.substring(index1, index2) and s.indexOf(searchString)

b.    new Scanner(s)

c.    s.split(delim)

                                                            iii.     Reading numbers

1.    scanner.nextInt() doesn’t handle newline characters

    1. Command line arguments

                                                               i.     Why now?

1.    Often used for passing filenames into programs

2.    Can result in/throw exceptions

  1. Exercise - EmailReader.java
  2. A more general view of exception handling
    1. Detection

                                                               i.     Recognizing that an exception has occurred.  For instance, unable to open the filename specified.

    1. Handling

                                                               i.     Doing something productive with that exception

                                                             ii.     What exactly this means will depend on the context of the exception

1.    Different exceptions/situations require different handling.

    1. Exceptions are Classes that we create objects from.  There is a hierarchy of exceptions that we will discuss in a week or two.  Some are more specific versions of others while some, such as Exception, are very general.
  1. Step one - detecting
    1. When we detect an Exception – Throw it!
    2. Syntax:

if(/*Something bad happens*/) {

      throw new <Type of Exception>();

}

 

    1. Note – we use the keyword ‘throw’ here, not ‘throws’
    2. The above code does not handle the exception – it only throws it.
    3. Note – exceptions are themselves objects.  We can call methods from them.  We’ll see more of this later.  We can usually construct them with an error message, something like:

if(/*Something bad happens*/) {

      throw new <Type of Exception>(“Abort! Something bad happened!”);

}

 

  1. Step two - handling
    1. When an exception is thrown, execution continues with the appropriate exception handler.  This may or may not be in the same method/class.  It will go in order down the call stack until a handler is found.
    2. If an exception is unhandled, the program will end, and display an error message.
    3. We handle exceptions via try/catch blocks.

                                                               i.     try – we’ll try to execute some code.  It might throw an exception before it is completed.

                                                             ii.     catch – we’ll catch a specific type of exception that might occur in the code, and handle it accordingly.

    1. Syntax:

try {

      //Code that might cause exception

}

catch(<Type of exception> <object name>)

{

      //Do something, such as:

      //<objectName>.printStackTrace();

}

 

    1. Each ‘catch’ block can handle a certain type of exception, such as FileNotFoundException or IOException.
  1. Different types of things that can go wrong.
    1. Errors

                                                               i.     These are issues related to our computer or something else, such as running out of memory.  We won’t worry about these.

    1. Unchecked Exceptions

                                                               i.     These are exceptions that are ‘preventable’.  They are the result of buggy code.

                                                             ii.     For instance: ArrayIndexOutOfBoundsException, NullPointerException

                                                            iii.     Java does not require us to handle these

    1. Checked Exceptions

                                                               i.     These are exceptions that we can’t predict

                                                             ii.     For instance: FileNotFoundException, IOException

                                                            iii.     Java requires us to handle these

    1. All checked exceptions must be handled somehow

                                                               i.     If we say that the method throws that exception, it will be passed up the call stack to the method that called this method.

                                                             ii.     If we handle the exception with a try/catch block, we do not keep searching up the call stack – the JVM is satisfied.

                                                            iii.     If there is an exception that can be thrown by the code in the try block, but it is not handled in a catch block, we still must acknowledge that the method throws that exception – we will then search up the call stack hierarchy.

  1. Homework – For Wednesday – Finish Program 3