8

What is the difference between

try {
     // action A
}
catch(Exception e) {
     // action B
}
finally {
     // action C
}

and

try {
     // action A
}
catch(Exception e) {
     // action B
}
// action C

I have read that you can return from inside a catch block and still have the finally block execute. Are there any other differences?

Pops
  • 30,199
  • 37
  • 136
  • 151
Sidor
  • 89
  • 2

6 Answers6

9

Things that happen within the finally block are guaranteed to occur no matter what happens in the try-catch-block. If an exception happens that is not encapsulated by Exception (e.g., extends Throwable, such as various Errors), then it still runs the finally block.

One thing to be aware of: if, within the finally block, a RuntimeException is thrown, or another Exception escapes from within it, then the rest of the finally block will not execute. Also, as Lord Torgamus pointed out, it is contingent on the JVM running. In addition, and probably obviously, it is also contingent on the thread not being stopped.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
pickypg
  • 22,034
  • 5
  • 72
  • 84
  • 1
    -1 because this isn't _quite_ true. It's a grammar nitpick, but in this case it's a relevant grammar nitpick. Not all things inside the `finally` are guaranteed to occur. Execution of the block is merely guaranteed to start __if__ the JVM doesn't shut down beforehand. – Pops Apr 29 '11 at 15:46
  • Addendum, for those users who care about such things: I removed my downvote after this was edited, but I left the comment because it's referenced in the post. – Pops Apr 29 '11 at 18:28
5

Most of the existing answers contain pieces of the right answer, but none are quite spot-on.

The finally block is always guaranteed to be reached after the try and potentially catch blocks if the JVM does not shut down beforehand. However, if a bit of code inside the finally block shuts down the JVM, or throws an exception its own, the end of the block might not be reached.

Per the Sun Certified Programmer for Java 6 Study Guide:

  • The only exception to the finally-will-always-be-called rule is that a finally will not be invoked if the JVM shuts down.

  • Just because finally is invoked does not mean it will complete.

The final word is, as always, the Java Language Specification. The behavior of finally is explained exhaustively in §14.20.2 Execution of try-catch-finally.

As an extra note: you're right that a return in the try won't stop finally from running. In fact, finally is entered immediately after the return is encountered, before it executes.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • 1
    So basically you just took everyones answers (while down voting them) and compiled them into your own. Welcome to SO. – Zombies Apr 29 '11 at 17:44
  • @Zombies, if you must know, I originally wasn't even going to answer; I was downvoting all the ones that were incomplete and was going to upvote the good one (yes, yours). But then I noticed that the one I thought was good was also incomplete. And even if that hadn't been the case, replacing multiple mediocre/poor answers with one good one is considered a good thing here. – Pops Apr 29 '11 at 18:26
  • So if the thread stops (but the JVM continues) inside the `catch` block, the `finally` block will still execute? – Blueriver Aug 28 '15 at 20:55
2

Better look at this example, resources are allways closed even if I return or propagate the Excpetion up.

try {
 //action A
 return 0;
} catch (Exception e){
 //action C
 throw new Excpetion("Probleme here",e)
} finnaly {
 //action C
 resources.close();
}

If action A and be are as primitve as int a = 0, then there is no difference, but in more complicated situations like this, finnaly block have it's usage

Hurda
  • 4,647
  • 8
  • 35
  • 49
1

from the Sun Tutorials

Note: If the JVM exits while the try or catch code is being executed, then the finally block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block will not execute even though the application as a whole continues.

I don't know of any other ways the finally block wouldn't execute...

Deminem
  • 672
  • 8
  • 19
0

Code inside the finally block is guaranteed to execute should the JVM continue running.

This means that even if what is in action B throws another new exception or doesn't catch the exception in action A or a return is called, the code in action C will run.

Zombies
  • 25,039
  • 43
  • 140
  • 225
  • I like your attention to detail :) – josh.trow Apr 29 '11 at 15:45
  • Agh, I almost upvoted this and then realized it's also incomplete. `finally` can itself throw an exception or shut down the JVM, which would potentially cause later code in the same block to not get executed. – Pops Apr 29 '11 at 15:50
  • @Lord Torgamus: Anyone can figure that out on their own without writing it out explicitly. If they couldn't figure that out there would be larger problems then the format this answer was written in! – Zombies Apr 29 '11 at 17:41
0

A try block is used for exception handling. If there any case/code that will throw exception, e.g., if we divide a number by Zero(0) than it will throw an exception and the process will be shutdown. In this case if we put our code in the try block than the exception is catch by the catch block and the process will not be terminated. And the finally block the guarantee of executing the code written there. Therefore, if we have to terminate/close the process even its execution is successful or not(e.g., in case of network programming we have to release the connection at last so that other devices can use that connection) we use the finally block.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117