Reputation: 2046
I was wondering what's the difference between code inside finally block and code after finally block
Upvotes: 7
Views: 8461
Reputation: 96424
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 program throws an exception, the JVM executing the program catches it and prints it out.
What 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 exception is thrown the only things that that thread will execute (until the exception is caught by the JVM) are finally blocks (where the exception is leaving a try-block that the finally belongs to).
Upvotes: 11
Reputation: 1047
Here I am adding more to the example provided above and may help someone in the future and hope will avoid some confusion.
Lets Start.
Actually, it depends on the flow control of the program. That means if the program is written in such a way that if your code is handling the exceptions thrown in the try blocks and if you are handling them in the catch block, then your code after finally block will get executed after the code inside the finally block.
Example 1: here the exceptions have been handled and hence the 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 the exceptions in the try block have not been handled properly as told by Nathan above, then the code after the finally block 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 sum up, the code inside the finally block gets executed always except in some cases where the thread has been stopped or killed before the finally block or in case if there are any exit programs written in a try block. Whereas the code after finally depends on the code written in try-catch-finally blocks and the exception handling.
Upvotes: 3
Reputation: 7709
If you throw the exception on the catch block, or if you return something on your try block, it will execute the finally block. If not on the finally block (after try/catch/finally) it wont work. Here is a simple example for you to understand: Try it without the finally block and you will see that the line where it prints the message "Closing resources..." will never be executed.
try {
return "Something";
} catch (Exception e) {
throw e;
} finally {
System.out.println("Closing resources (Connections, etc.)...");//This will always execute
}
Upvotes: 2
Reputation: 663
If the catch block re-throws an exception (or throws a new one), the finally block is executed. Anything after the finally block will not be executed.
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. - http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Upvotes: 4