• 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 in try/catch

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

in this code what is the use of LINE01 and LINE02 ...as I tried .. what ever be the case .. it returns "return value" of finally clause only.
TIA
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravish,
In your example, the finally block will be the last piece of code to execute when:
1. no exceptions are thrown
2. an exception is thrown but not caught
3. an exception is thrown and is caught
So, just to confirm your query, the method will always return int 3.
chung
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravish,
In your example, the finally block will be the last piece of code to execute when:
1. no exceptions are thrown
2. an exception is thrown but not caught
3. an exception is thrown and is caught
So, just to confirm your query, the method will always return int 3.
chung
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is OK ..
but what is the use of having three return stmt..
or this is just for certification ... no real time use ???
TIA
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line with this question (it's not all clear to me too), I once saw a question-fragment like:

What does this mean?
Curiously waiting for an answer..Erik Dark
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you are on the right lines in thinking that the return statements in the try and catch blocks are redundant. I checked it out by commenting out these return statements and the class compiles OK. I guess that this piece of code was used for certification purposes to test the knowledge of finally.
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravish,
I agree with you, the return statements in the try and catch block are redundant and the compiler will not complain when they are commented out. I think they were included in the test to challenge our knowledge.
Erik,
Please check the JLS for the full description of return.
Basically,
return; // will return nothing and is often used as the last statement of a method/block to signal the end of the method/block.
return anyValue; //will return the variable value or reference object at the end of the method.
If you have a finally block after the try block, the finally block will be executed before the method returns (unless your computer dies, or System.exit() is called etc.)
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravish kumar:

in this code what is the use of LINE01 and LINE02 ...as I tried .. what ever be the case .. it returns "return value" of finally clause only.
TIA


OK ... so now for exam :
FYI:
if you remove comment from LINE03.. then compiler will give error as unreachable stmt ..... but then if u comment any one return stmt then it will compile...
HTH
CMIW
 
reply
    Bookmark Topic Watch Topic
  • New Topic