Java Exception

Java Exception Handling
Keywords involved:

    try
    catch
    finally
    throws
    throw


Example:
public void method throws xxxException,xxException,... {

     try {
       //..
       
     }catch(xxException e) {
       //..
       throw new xxxException("xxx");
     }catch(Excepiton ee) {
       //..
     }finally {
       try {
        //..
       }catch(xxException eee) {
        //..
       }
     }
     //..
}
----------------------------
finally keyword

finally block will be definitely executed
except System.exit() called or thread stop running before 
the finally block.
----------------------------
Structure
                        Throwable
                            |
                       ---------------    
                      |               |
                  Exception       Error
                      |
               -----------------      
              |                |
  checked Exceptions   RuntimeException  
 
 
---------------------------------
Error

Errors generally describe problems that are sufficiently 
unusual and sufficiently difficult to recover from.
JVM-related failure. e.g. running out of memory.

You are not required to handle Errors.

--------------------------------
Runtime Exception or unchecked exception

Typically describe program bugs.

e.g. ArrayIndexOutOfBoundsException
 since it can be avoided by correctly coding program
 
e.g NullPointerException, like attempting to access object 
fields or call object methods via object reference variables that 
contain null values 

e.g ArithmeticException, like integers divided by zero

You are not required to handle Runtime Exception or unchecked exceptions

---------------------------------
Checked Exceptions
  
--Exceptions that must be dealt with in a program; the compiler checks 
  to ensure the program handles them. 

--Any subclass of Exception except subclasses of runtime 
  exception is a checked exception
  
----------------------------
How to Deal With Checked Exception

Two options:

--put a try block around the code that might throw the exception 
  and catch related exception.
--Add throws in the method header to let potential callers to 
  deal with it.
  
----------------------------
If a method throws an exception(only checked exception), 
that exception must be caught or declared to be thrown.

For example, any method in File class throws IOException,
when you use those methods, you can:

option 1 (declared to be thrown)
public void openFile() throws IOException {
     File f = new File(“xxx.txt”);
     f.xxx();
} 

option 2 (try/catch it)
public void openFile() {
     try {
        File f = new File(“xxx.txt”);
        f.xxx();
     }catch(IOException io) {
        System.out.println(io.getMessage());
     }
}

-------------------------------------
Exceptions vs. Overriding

Overriding method cannot be declared to throw checked exceptions 
other than those that were declared by the method in the super class.

For example:
void method() throws IOException{}//in your super class

Your overriding method can be one of them:
void method() throws IOException{}//throw same exception
void method (){}    //don't throw any exception
void method() throws EOFException, ArrayIndexOutOfBoundsException{}
//EOFException is subclass of IOException, ArrayIndexOutOfBoundsException
//is a runtime exception(unchecked exception)

---------------------------------
StackTraceElement class

getClassName(),
getFileName(), 
getLineNumber(), 
getMethodName(),
etc.

printStackTrace();==> get elements of stack trace

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
  at SortIntegerArray.sort(SortIntegerArray.java:19)
  at SortIntegerArray.main(SortIntegerArray.java:9)

---------------------------------

create our own exception

e.g.

class WrongInputException extends Exception {
    WrongInputException(String s) {
        super(s);
    }
}

class Input {
    void method() throws WrongInputException {
        throw new WrongInputException("Wrong input");
    }
}

class TestInput {
    public static void main(String[] args){
        try {
            new Input().method();
         }catch(WrongInputException wie) {
            System.out.println(wie.getMessage());
        }
    } 
}

>javac TestInput.java
>java TestInput
Wrong input

---------------------------------

Lab 
1. book example, Questions
2. modify GuessNumber.java
3. project 5