This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is the purpose of throwing exception from catch block and finally ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the purpose of throwing exception from catch block and finally ?" Watch "What is the purpose of throwing exception from catch block and finally ?" New topic
Author

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

Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20
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

Joined: Oct 13, 2005
Posts: 39079
    
  23
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 18, 2012
Posts: 20
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

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