Lecture 37, CS 302-6, November 30

  1. Various things
    1. Program 2

                                                               i.     Grades online – forms page

    1. Program 4

                                                               i.     Coming soon – stay tuned

  1. Review:
    1. Exceptions

                                                               i.     Signal that something went wrong

                                                             ii.     Some examples:

1.    ArrayOutOfBoundsException

2.    NullPointerException

3.    ArithmeticException

    1. Detecting Exceptions

                                                               i.     If something goes wrong, Throw an exception

                                                             ii.     Either one of the existing ones, or we can create our own

                                                            iii.     Syntax:

if(/*Something bad happens*/) {

throw new <Type of Exception>();}

                                                           iv.     throw vs throws

    1. Handling Exceptions

                                                               i.     When an exception is thrown, execution continues with the appropriate exception handler.

                                                             ii.     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.

                                                            iii.     Try/catch

                                                           iv.     Syntax:

try {

//Code that might cause exception}

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

{

//Do something, such as:

//<objectName>.printStackTrace();}

 

    1. Types of things that can go wrong

                                                               i.     Errors

1.    Out of our control – eg out of memory

                                                             ii.     Unchecked Exceptions

1.    Runtime exceptions that we should have prevented

                                                            iii.     Checked Exceptions

1.    Exceptions that we can’t foresee, but we must handle

  1. Exceptions as objects
    1. As mentioned, Exceptions are themselves objects
    2. We can tell because we new them when we throw them
    3. They don’t behave quite the same as other objects, though
    4. Instead of passing them, we throw/catch them
    5. Constructors

                                                               i.     ()

                                                             ii.     (String message)

    1. Methods we can call from Exceptions

                                                               i.     getMessage()

1.    Returns the message stored in the exception

                                                             ii.     printStackTrace()

1.    prints out current call stack at time of exception

  1. Exception philosophy
    1. Exceptions aren’t necessarily a ‘bad’ thing – we can use them to our advantage
    2. In general – we should only attempt to handle an exception when we know the proper thing to do with it.
    3. Don’t be hasty to deal with it unless you have to – good programmers are lazy
  2. Catching exceptions
    1. You can catch unchecked exceptions, such as array index out of bounds or input mismatch exception
    2. Option one: catch locally (in the same method)

                                                               i.     Error.java

1.    OutOfMemoryError

2.    Can’t catch…

                                                             ii.     UncheckedException.java

1.    ArithmeticException and InputMismatchException

2.    In general, you should prevent these sorts of errors, not handle them

3.    Note – we can have multiple catches for one try.  They are evaluated top to bottom

                                                            iii.     CheckedException.java

1.    FileNotFoundException

    1. Option two: catch remotely (in method from call stack)

                                                               i.     Exception is thrown in called method

                                                             ii.     Then, it is caught in the calling method

                                                            iii.     ExceptionsExample.java

    1. Generic Exception – catch(Exception e){}

                                                               i.     Will catch any exceptions

  1. What if you don’t handle an exception?
    1. If you don’t catch an exception, and it makes it all the way down the call stack (past main) – runtime crash

                                                               i.     Note – this can only happen with unchecked exceptions, or if we keep saying that a method throws an exception (without handling it)

    1. Exception message – What happened?
    2. Call stack trace – Where did it happen?