• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

returning a value from try and finally

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried that code? Have you read about what happens in the Java Language Specification?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic