12/14/2008

Exceptions

Exception = error in the flow


Types of exception

Checked exception     : Subclasses of the Exception class (except RuntimeException)
Runtime exception     : The RuntimeException class and its sublcasses
Error     : The Error class and its subclasses

Runtime and Error are unckecked exceptions
_______________

try - catch - finally block

The try - catch - finally block can handle checked exceptions.
The operation written in the finally block runs regardless of whether an exception is caught or not.

public class Test {
    public static void main(String[] args) {
        Test test = new Test ();
        test .func();
    }

    /* exceptionMethod will be caught in the try - catch - finally block */
    void func() {
        try {
            exceptionMethod();
        } catch (Exception e) {
            System.out.println("Exception occurred");
        } finally {
            System.out.println("Necessary process");
        }
    }

    /* Declare the possibility of exception by 'throws' */
    void exceptionMethod() throws Exception {
        throw new Exception();
    }
}
_______________

You have to either handle an exception or throw an exception.

<When you handle an exception>
    use try - catch 
<When you throw an exception>
    By using the keyword 'throws', declare the possibility that an exception might occur
_______________

throw 

You can literally throw an exception by using the keyword 'throw'.
You can throw the exception caught in the try - catch block back to the invoker.

    void func() throws Exception {
        try {
            exceptionMethod();
        } catch (Exception e) {
            throw e;
        }
    }

Then you have to use the try - catch block in the invoker.
_______________

When you handle multiple exceptions:

You have to make sure the try - catch block handles exceptions from the lower level one in the hierarchy of Java classes.
Otherwise, a compile error occurs.

    void func() {
        try {
            exceptionMethod();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    void exceptionMethod() throws IOException {
        throw new IOException();
    }

If you write 'Exception' first in the catch block, the IOException block cannot be reached and thus the compile error occurs.
_______________

RuntimeException
You do not have to declare the possibility that a RuntimeException might occur by the 'throw' keyword because the RuntimeException class is an unchecked exception.

    void func1() throws IOException {
        throw new IOException();
    }
    void func2() {
        throw new RuntimeException();
    }
_______________

finally 
What you write in the finally block is carried out regardless of whether an exception occurred, and thus it is an appropriate place to write the process of cutting off the stream.
However, the code below cannot be compiled.

public class Test{
    public static void main(String[] args) {
        BufferedReader reader = null;
        try {
            reader  = new BufferedReader(new FileReader("C:/sample.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
    }
}