• 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

behaviour of finally

 
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program.



In the above programme the output is 3 rather than 1.
When we use a return statement the control should be returned to main but how the output is 3 ???
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the finally block is always executed. It returns 3.
 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Campbell Ritchie,

Thanks for answering.

In the same program in place of in try block if we have The system is exiting with out executing the finally block.

Should the above scenario be considered a special case?


-Aruna.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aruna sydu wrote:
In the same program in place of in try block if we have The system is exiting with out executing the finally block.

Should the above scenario be considered a special case?




I think that it is safe to add the phrase, "unless the JVM terminates", to almost every guarantee in java...

Henry
 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Henry Wong,

Thanks for the answer.


-Aruna.
 
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
I can't resist pointing out that this means that using "return" in a finally block is virtually always the wrong thing to do -- don't do it!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aruna sydu wrote:
Hi Campbell Ritchie,

. . . if we have [code]System.exit(0); The system is exiting with out executing the finally block.

Should the above scenario be considered a special case?


-Aruna.

Yes, (as Henry has already said), that probably is a special case because the try block is never completed. For other reasons for a finally not being executed, searchm and you find this thread (there are others, too)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi aruna,


It will not return 1 ,because finally block will always excute.
If you want the value to be returned as 1, then remove the finally block.
 
reply
    Bookmark Topic Watch Topic
  • New Topic