File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes try, catch,finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try, catch,finally" Watch "try, catch,finally" New topic

try, catch,finally

Vsam hr

Joined: Dec 25, 2009
Posts: 5
I just tried the following try-catch-finally code. When i executed it, the output printed was

My question is When does the 'finally' block exactly get executed? Becuase it prints all that is there is 'try' block skips the 'return 2' statement and executes 'finally'. How does it skip that particular statement? I understand that finally is always executed even when there is exception but managing to skip exactly that particular 'return' statement in 'try' block is what puzzles me..

Is there some priority stuff in this meaning... does try-catch-finally block has more priority than a 'return' statement?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

It doesn't skip the "return 2" -- it overrules it with "return 4". If a finally block returns a value or throws an exception this will overrule the return value / exception from the try or catch block. For example, the following example shows no output at all -- the exception is simply ignored.

How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The way to think about it is just that the code in a "finally" block always executes after the corresponding try block (and after any catch blocks that are used). The only time a "finally" block is skipped is if the thread that's executing the code is terminated, which will happen only if System.exit is called, if the JVM crashes, or if someone calls the "Thread.stop()" method. There are no notions of priority.

This means, if you think about it, that putting "return" into a finally block is virtually always a very bad idea. As you see here, if both a try and a finally include "return", then the try block's return value is ignored.

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

I agree with Ernest about returning from a finally block. If you need to return the same value regardless of whether or not the try block is successful or not, you should put the return statement after the try-catch-finally.
Unnikrishnan Nair
Ranch Hand

Joined: Mar 12, 2003
Posts: 42
Guys, consider finally as a cleanup place where you release back the resources which you might have acquired in try block. Its a bad practice to return from finally

SCJP, SCWCD, SCBCD, SCDJWS, SCSNI, SCEA 5, PMP, Six Sigma Green Belt, Certified Scrum Master
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Have a look on this, wonderful explanations!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
akhilesh bar

Joined: Sep 10, 2010
Posts: 4
Hi Huk

you are right that the finally block always executes even if there is an mind it that finally always executes at the end of all the blocks right..
According to your code first try block executes and prints HELLO then the return 2 statement executes but wait a minute know the finally block is still there to before going back to the calling statement it will execute the finally block and will return 4 by overwriting (you can say replacing 2 from 4)....stored in a..thus the output

I think these steps made some doubts clear to you !!

Am i right seniors???

@ {< {< !
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Yes you are.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Link given above is very good.
Must read it.

Happy Learning

SCJP6.0,My blog Ranchers from Delhi
I agree. Here's the link:
subject: try, catch,finally
It's not a secret anymore!