• 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

Return Statement from a Method

 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If a Method is suppoused to return a Value , the return value should be always in a finally block ? Am i correct on this ?


 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you generally don't need to put return values in finally blocks. Finally blocks are usually used for resources that need to be released, such as closing files or database connections. Your method could easily be rewritten as:

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John Thanks ,

If its a simple what you said would be fine . But if i am dealing with any Data Base connections then i should be dealing with the try and catch blocks in that case i am asking.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the reasons/purposes of having a try/catch/finally block is to separate out error handling from the normal flow of your code. the normal operation goes in the 'try', the error goes in the 'catch', and the 'i really need to clean up' goes in the 'finally'.

Since the return is part of the normal flow, it could go in either the 'try' or outside of the try/catch block all together. some folks argue that the ONLY think that should go in the try is stuff that can throw the exception, and your return statement is not one of them.

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should even be careful about returning values from finally. For example:

The above code will print 5, not 1.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever you place in Finally will run regardless of what happens as a result of your try/catch block so only put code in the finally block that you always want to run.
 
No holds barred. And no bars holed. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic