What is the purpose of throwing exception from catch block and finally ?
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ?
Is there a special purpose to this ?
What is difference in throwing exception from try and catch and finally ?
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
why we are using catch to throw exceptions like is it for convenience or something else ?
and why not to through exception from finally ?
and why not to through exception from finally ?
posted 12 years ago
As I said: don't know. How we can we even guess with so little information?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
posted 12 years ago
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.
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
| Bring me the box labeled "thinking cap" ... and then read this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |













