Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning a value from try and finally

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vikash Ananda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried that code? Have you read about what happens in the Java Language Specification?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic