Behavior of nested finally in Exceptions

Posted by kuriouscoder on Stack Overflow See other posts from Stack Overflow or by kuriouscoder
Published on 2011-01-12T02:47:01Z Indexed on 2011/01/12 2:53 UTC
Read the original article Hit count: 197

Filed under:

Hello:

Today at work, I had to review a code snippet that looks similar to this mock example.

package test;

import java.io.IOException;
import org.apache.log4j.Logger;

public class ExceptionTester {

    public static Logger logger = Logger.getLogger(ExceptionTester.class);

public void test() throws IOException {
    new IOException();
}

public static void main(String[] args) {
    ExceptionTester comparator = new ExceptionTester();
    try {
        try {
            comparator.test();
        } finally {
            System.out.println("Finally 1");
        }
    } catch(IOException ex) {
        logger.error("Exception happened" ex);
            // also close opened resources
    } 
    System.out.println("Exiting out of the program");
}

}

It's printing the following output.I expected an compile error since the inner try did not have a catch block.

Finally 1
Exiting out of the program

I do not understand why IOException is caught by the outer catch block. I would appreciate if anyone can explain this, especially by citing stack unwinding process

© Stack Overflow or respective owner

Related posts about java