Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

understanding try-catch block?

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can return a value in try, catch and finally block. You just need to remember that finally block will always be executed...
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also finally will suppress any return in catch block.. For instance the below code will print 5.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all of you.I have understood the concept.
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vinay Dinakar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic