aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return in try/catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return in try/catch" Watch "return in try/catch" New topic
Author

return in try/catch

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371

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


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
chung lee
Ranch Hand

Joined: Jan 17, 2002
Posts: 33
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

Joined: Jan 17, 2002
Posts: 33
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

Joined: Oct 15, 2001
Posts: 5371
that is OK ..
but what is the use of having three return stmt..
or this is just for certification ... no real time use ???
TIA
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
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

Joined: Jan 17, 2002
Posts: 33
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

Joined: Jan 17, 2002
Posts: 33
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

Joined: Oct 15, 2001
Posts: 5371
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: return in try/catch