| Author |
understanding try-catch block?
|
Sonali Sehgal
Ranch Hand
Joined: Jul 09, 2009
Posts: 75
|
|
can we write return statement in try catch or finally block.
supose we write return 0 in try catch or finally. is it right or wrong?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
It is OK to write return in try{}catch{}finally{} block. We can write return; (when a function is declared as void) or return typeValue; (when a function is declared as not void).
The returned value will be 2.
Result:
Returning value: 1
Returning value: 2
Returned value: 2
Very interesting thing happens when we write return inside finally block:
The above code is OK! The exception is NOT handled, but it is CONSUMED!
|
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same
|
 |
karthikeyan Chockalingam
Ranch Hand
Joined: Sep 06, 2003
Posts: 259
|
|
And also finally will suppress any return in catch block.. For instance the below code will print 5.
|
http://www.skillassert.com
|
 |
Sonali Sehgal
Ranch Hand
Joined: Jul 09, 2009
Posts: 75
|
|
|
Thanks a lot to all of you.I have understood the concept.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Lukas Smith wrote:
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same
But you don't need to worry about that in SCJP ...
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Shanmuga Priya wrote:And also finally will suppress any return in catch block.. For instance the below code will print 5.
I have just figured this out
Sonali - you are welcomed!
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Ankit Garg wrote:
Lukas Smith wrote:
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same
But you don't need to worry about that in SCJP  ...
System.exit(0) can be tested during SCJP. Stay tuned.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
I was only trying to say that I've never seen a question combining try catch block and System.exit. So generally you can say that finally block is sure to be executed...
PS: Bert can confirm on that more...
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Ankit Garg wrote:I was only trying to say that I've never seen a question combining try catch block and System.exit. So generally you can say that finally block is sure to be executed...
PS: Bert can confirm on that more...
"Never say never..." - from the song
Just a joke
|
 |
Rashmi Liyan
Greenhorn
Joined: Aug 16, 2009
Posts: 9
|
|
Lukas Smith wrote:It is OK to write return in try{}catch{}finally{} block. We can write return; (when a function is declared as void) or return typeValue; (when a function is declared as not void).
Hi Lukas,
can you exlain this little bit. I didn't get it.
Rashmi.
|
 |
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
|
|
Lukas Smith wrote:
Ankit Garg wrote:
Lukas Smith wrote:
Ankit Garg wrote:Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
Not at all. System.exit(0) will cause that finally will not be executed and internal error/exception of JVM can cause the same
But you don't need to worry about that in SCJP  ...
System.exit(0) can be tested during SCJP. Stay tuned.
You usually put in a finally block the code that must happen no matter how the try block is exited. So thinking of System.exit(0) or JVM internal error/exception is not important for me when i write finally block.
|
http://muhammadkhojaye.blogspot.com/
|
 |
Vinay Dinakar
Greenhorn
Joined: Dec 30, 2008
Posts: 17
|
|
System.exit(0) makes control to return without executing finally block (Which is having return statement). So now there is no return value, why doesn't compiler throws an error ?
|
Thanks,
~ Vinay Ds
|
 |
Jehaan Butt
Ranch Hand
Joined: Feb 05, 2009
Posts: 41
|
|
System.exit(0) makes control to return without executing finally block (Which is having return statement). So now there is no return value, why doesn't compiler throws an error ?
Well, here your try block is not throwing an exception, due to which there will be no internal JVM error. The System.exit(0) merely exits the program, and that does not cause any error.
This version of code does give an internal JVM error, because there is an internal exception lurking around the JVm which has not been handled.
|
SCJP 6
|
 |
Vinay Dinakar
Greenhorn
Joined: Dec 30, 2008
Posts: 17
|
|
|
Ok. Thats fine. But my question is , compiler sees that test method return type is "int". But there is no "return" statement gets called here. So Why compiler doesn't say like "return statement is missing" ?
|
 |
Jehaan Butt
Ranch Hand
Joined: Feb 05, 2009
Posts: 41
|
|
Ok. Thats fine. But my question is , compiler sees that test method return type is "int". But there is no "return" statement gets called here. So Why compiler doesn't say like "return statement is missing" ?
My guess (and it is only a guess) is that the compiler doesn't realise that either of the two return calls will not be reached. It sees that the System.exit(0) call is reached and is happy, because when I tried throwing an exception before the call it complained. It does not actually work out the complete code logic and since both the catch and finally have return statements, it feels that there is no problem.
|
 |
 |
|
|
subject: understanding try-catch block?
|
|
|