aspose file tools*
The moose likes Beginning Java and the fly likes returning a value from try and finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning a value from try and finally" Watch "returning a value from try and finally" New topic
Author

returning a value from try and finally

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
if I have a return value both from try and finally then the value returned from finally is taken.

for ex here is a code snippet

public int returnint () {
try {
return 1;
} finally {
return 2;
}
}

this code will return 2.my question is why java has allowed this and why does not it generate compile error if we want to return some value from the finally block.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
I have heard somewhere that jvm merges these return statements behind the scene and finally return is retained, if present. But This is not detailed explanation and you need to look further.
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
please consider it this way whenever there is an exit from the try block the finally will be executed. I dont think so there is any mearging. it simple as JVM encounters a statement or end brace that says "Get out from my try block", the JVM will always execute the finally block. its like when you tell a person to leave your house, he will prefer packing up his luggage before leaving. its just the same before leaving the try block the control packs the luggage i mean the finally block is executed
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

nitinram agarwal wrote: why java has allowed this and why does not it generate compile error if we want to return some value from the finally block.


Indeed -- many people consider this a defect in the language specification. Some compilers -- Eclipse's Java compiler is one -- will warn you about this.


[Jess in Action][AskingGoodQuestions]
Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
finally is used when you need to execute something that should be executed at all times invariable to what happens in try block. refrain from creating such codes as you illustrated
nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
I agree that we should not write such code but am curious to know the reason as I was asked this question in one of the technical discussion that I had and I tried to reason out the same but the interviewer was curious to know some particular response which I am not sure what.
so thought of posting this question.
Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101
Ernest Friedman-Hill wrote:
nitinram agarwal wrote: why java has allowed this and why does not it generate compile error if we want to return some value from the finally block.


Indeed -- many people consider this a defect in the language specification. Some compilers -- Eclipse's Java compiler is one -- will warn you about this.


NetBeans also warns that

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Have you tried that code? Have you read about what happens in the Java Language Specification?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning a value from try and finally
 
Similar Threads
Error in class level variable
Doubt in try and finally block execution.
try, catch,finally
try, catch, finally and return
Return Statement and Finally