Thursday, April 14, 2005

Java finally block

Just had a quick discussion about finally blocks in Java code. If an exception is thrown in the catch block does the finally block still run?
class Test
{
        public static void main(String argv[]) throws Exception
        {
                try
                {
                        System.out.print("Hello");
                        throw new Exception("E");
                }
                catch (Exception e)
                {
                        throw new Exception("F");
                }
                finally
                {
                        System.out.println(" World");
                }
        }
}
The answer is:
java Test
Hello World
Exception in thread "main" java.lang.Exception: F
        at Test.main(Test.java:12)