• 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

What is the purpose of throwing exception from catch block and finally ?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of throwing exception from catch block and finally ?
Is there a special purpose to this ?
What is difference in throwing exception from try and catch and finally ?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know the difference between try catch and finally? I suggest you start in the Java Tutorials, but as a rule of thumb, don’t throw any Exceptions in a finally.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With regards to catch, I think you are going to have to give us some concrete examples before an answer is possible. Sometimes that's just flat-out poor programming; other times, it could be necessary to convert an exception from one type to another. Who knows? We can't tell without seeing the code.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should never throw an exception from finally. You should also not return, break, or continue out of finally. We want the completion of the overall try statement to be determined by the parts doing the work--the try can catch blocks--not by the part that cleans up after the real work is done.

As for throwing from within try or catch, that's no different from throwing anywhere else. We do it when something happens that is outside the expected "happy path" execution sequence.
 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why we are using catch to throw exceptions like is it for convenience or something else ?
and why not to through exception from finally ?
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rasul Patrick wrote:why we are using catch to throw exceptions like ...


As I said: don't know. How we can we even guess with so little information?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rasul Patrick wrote:why we are using catch to throw exceptions like is it for convenience or something else ?



Usually when we throw from within catch it's because we're wrapping the existing exception (the one that got us to the catch block) with a different exception that's more appropriate for the caller of our method, to hide implementation details that aren't relevant to our caller.

We might also do it if a problem occurs as we're handling the exception. After all, a catch block is code, and things can go wrong when we execute code, and the way to indicate something went wrong is to throw an exception.

and why not to through exception from finally ?



If our try has completed successfully, we want our method to complete successfully, so that we get our results. All the work that we care about, the whole reason we called the method, is done and completed successfully. If there's some problem as we're cleaning up, the caller doesn't care about that, so finally shouldn't throw an exception.

On the other hand, if our try block failed, we want to know about that error. We don't want something that goes wrong in finally to give us a different exception.

 
I've got no option but to sell you all for scientific experiments. Or 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