[ACCEPTED]-What's the difference between code inside finally block and code after finally block?-finally

Accepted answer
Score: 11

A small test program shows the difference:

public class FinallyTest {

    public static void main(String[] args) throws Exception {
        try {
            boolean flag = true;
            if (flag) {
                throw new Exception("hello");
            }
        } finally {
            System.out.println("this will get printed");
        }
        System.out.println("this won't show up");
    }
}

The 8 program throws an exception, the JVM executing 7 the program catches it and prints it out.

What 6 gets written to the console is:

this will get printed
Exception in thread "main" java.lang.Exception: hello
    at snippet.FinallyTest.main(FinallyTest.java:7)

Once the 5 exception is thrown the only things that 4 that thread will execute (until the exception 3 is caught by the JVM) are finally blocks 2 (where the exception is leaving a try-block 1 that the finally belongs to).

Score: 2

If the catch block re-throws an exception 13 (or throws a new one), the finally block 12 is executed. Anything after the finally 11 block will not be executed.

The finally block 10 always executes when the try block exits. This 9 ensures that the finally block is executed 8 even if an unexpected exception occurs. But 7 finally is useful for more than just exception 6 handling — it allows the programmer to avoid 5 having cleanup code accidentally bypassed 4 by a return, continue, or break. Putting 3 cleanup code in a finally block is always 2 a good practice, even when no exceptions 1 are anticipated. - http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

Score: 2

If you throw the exception on the catch 8 block, or if you return something on your 7 try block, it will execute the finally block. If 6 not on the finally block (after try/catch/finally) it 5 wont work. Here is a simple example for 4 you to understand: Try it without the finally 3 block and you will see that the line where 2 it prints the message "Closing resources..." will 1 never be executed.

try {
    return "Something";
} catch (Exception e) {
    throw e;
} finally {
    System.out.println("Closing resources (Connections, etc.)...");//This will always execute
}
Score: 2

Here I am adding more to the example provided 24 above and may help someone in the future 23 and hope will avoid some confusion.

Lets 22 Start.

Actually, it depends on the flow control 21 of the program. That means if the program 20 is written in such a way that if your code 19 is handling the exceptions thrown in the 18 try blocks and if you are handling them 17 in the catch block, then your code after 16 finally block will get executed after the 15 code inside the finally block.

Example 1: here the 14 exceptions have been handled and hence the 13 code after finally, block executes.

public class TestFinally {

    public static void main(String[] args) throws Exception {
        try {
            boolean flag = true;
            if (flag) {
                throw new Exception("hello");
            }
        } 
        catch(Exception e){
            System.out.println("catch will get printed");   
        }
        finally {
            System.out.println("this will get printed");
        }
        System.out.println("this won't show up");
    }
}

result:

catch will get printed
this will get printed
this won't show up

Example 2: if 12 the exceptions in the try block have not 11 been handled properly as told by Nathan 10 above, then the code after the finally block 9 does not get executed.

public class TestFinally {

    public static void main(String[] args) throws Exception {
        try {
            boolean flag = true;
            if (flag) {
                throw new Exception("hello");
            }
        } 
        // not handling the exception thrown above
        /*catch(Exception e){
            System.out.println("catch will get printed");   
        }*/
        finally {
            System.out.println("this will get printed");
        }
        System.out.println("this won't show up");
    }
}

result:

this will get printed
Exception in thread "main" java.lang.Exception: hello
    at com.test.TestFinally.main(TestFinally.java:36)

So conclusively, to 8 sum up, the code inside the finally block gets 7 executed always except in some cases where 6 the thread has been stopped or killed before 5 the finally block or in case if there are any exit 4 programs written in a try block. Whereas 3 the code after finally depends on the code 2 written in try-catch-finally blocks and 1 the exception handling.

More Related questions