Lecture 38, CS 302-7 and 8, April 25

  1. Various things
    1. Program 4

                                                               i.     Uningenious

                                                             ii.     Partners – May 4

  1. Review:
    1. Types of exceptions

                                                               i.     Unchecked, checked, error

                                                             ii.     Error.java

    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…

a.    Recursive definition

                                                             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 from most specific to least specific

1.    Why?

2.    Generic Exceptions

a.    catch(Exception e)

b.    but, be careful – you shouldn’t just sweep exceptions under the rug…

    1. Exceptions as objects

                                                               i.     But, we throw them instead of passing them

                                                             ii.     Exception Constructors

1.    ()

2.    (String message)

                                                            iii.     Exception methods

1.    getMessage()

2.    PrintStackTrace()

  1. Exception Handling Example
    1. ExceptionsExample.java
    2. Watch out – don’t squelch exceptions.  Handle appropriately
  2. Finally blocks
    1. Code that always executes after a try, whether or not the try completed
    2. One use – closing scanners/printwriters
    3. Book approach

                                                               i.     Avoid try/catch/finally

                                                             ii.     Nested try/finally and try/catch

    1. Normal 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. Philosophy again – We’re getting out of the area of this is what you can/this is what you can’t do.
    2. Getting more into the area of this is what you should/shouldn’t do.
    3. 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
  1. Homework –Get started on Program 4!