Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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 ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic