Lecture 38, CS 302-6, December 02

  1. Various things
    1. Program 4

                                                               i.     http://en.wikipedia.org/wiki/Reversi

                                                             ii.     http://www.darkfish.com/turncoat/Turncoat.html

                                                            iii.     Partners - by Friday, 12/9

  1. Review:
    1. Exception Constructors

                                                               i.     ()

                                                             ii.     (String message)

    1. Exception methods

                                                               i.     getMessage()

                                                             ii.     PrintStackTrace()

    1. Try/catch

                                                               i.     Catching locally vs on call stack

1.    Rule of thumb – checked exceptions must be caught somewhere in a program.  So, a method that invokes it must either catch it or throw it, etc.  If it throws it, any method that calls it must either catch it or throw it.  Etc…

                                                             ii.     Catch specific Exceptions

                                                            iii.     Can have multiple

                                                           iv.     Exception Hierarchy

1.    http://www.programcreek.com/wp-content/uploads/2009/02/hierarchy.png

2.    In general – runtime exceptions are unchecked, ioexceptions are checked

                                                             v.     Should order catch blocks from most specific to least specific

  1. Using javaDoc
    1. When we are using methods/constructors, it’s helpful to know what exceptions they can throw, and whether those are checked or unchecked.
    2. The javadoc tells us this
    3. scanner.nextInt() vs new Scanner(File f)

                                                               i.     http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

                                                             ii.     If the method statement includes throws <Exception>, then it is checked

  1. Finally blocks
    1. Code that always executes after a try, whether or not the try completed
    2. One common use – closing scanners/printwriters
    3. Book approach

                                                               i.     Avoid try/catch/finally

                                                             ii.     Nested try/finally and try/catch

    1. Standard approach

                                                               i.     Try/catch/finally

                                                             ii.     Be careful to make sure you don’t get ‘might not be initialized’ compile-time errors

    1. FinallyExample.java
  1. Exception wrap-up
    1. Question – should your method throw a checked or unchecked exception?

                                                               i.     It depends

                                                             ii.     Unchecked might be ‘easier’

                                                            iii.     But, checked is often correct

    1. You’ll develop an intuition as to the right way to handle these sorts of things the more you program
    2. http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html
    3. http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
  1. Homework – For Monday – Get started on Program 4!