• 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

Does return in finally block eat away the exceptions?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please go through the following code snippet:-



If finally has a return statement then the exception is not being propagated, why is it so? Comment the finally block or just the return statement in the finally block. The code complies and runs showing the exception thrown. But if the above code is run with return in finally, it is like eating away the exception. Can any one explain this? please correct me if I am wrong.

[Edit: Put code between [code] and [/code] UBB Code tags. Didn't really help though ]
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A small change that I did to your code..As far as I think finally is executed after catch and the throw keyword is always used with the new keyword...
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you wanna see the throwable exception, then you can also explicitly terminate the program in the catch block. So, that your program will not execute the statement in final block.

Here's the little example:


 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the return statement in the finally block is usually not done. You cannot return and throw an exception at the same time so I guess the JVM chose to do what you wanted it to in the finally block.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the throw keyword is always used with the new keyword...



Sudipto, It is not mandatory to use new with throw but it turns out that majority of cases we have to throw new exception based on some logic.

Samrat , finally always supersedes the try catch block.
For example
1. execute the try block
2. if any error then go to catch block and then finally block
3. if no error than try and finally.

Also, a method can either throw a exception or return a value but not both at a time.. so from this and point 2 you can conclude the behavior.
 
Samrat Kakustam
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thank you everyone for your replies. Yes, it appears like a method can either throw en exception or return a value but not both at sametime.

I have planned to take SCJP this weekend. So in final phase of preparation.
Can you please sugesst me some good tough papers for practise? Any tips or sugesstions will be of great help.
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic