Java Exception

Java Exception Handling
Keywords involved:


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.
                      |               |
                  Exception       Error
              |                |
  checked Exceptions   RuntimeException  

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);;

option 2 (try/catch it)
public void openFile() {
     try {
        File f = new File(xxx.txt);;
     }catch(IOException io) {

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


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

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
  at SortIntegerArray.sort(
  at SortIntegerArray.main(


create our own exception


class WrongInputException extends Exception {
    WrongInputException(String 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) {

>java TestInput
Wrong input


1. book example, Questions
2. modify
3. project 5