Tell the difficulties that i am difficult.
kunal
saloni jhanwar wrote:
"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "
kunal
Kunal Lakhani wrote:I agree with Jesus Angeles.
Saloni jhanwar, post the complete matter and not the page number.
Rethrowing the Same Exception
Just as you can throw a new exception from a catch clause, you can also throw the same exception you just caught. Here's a catch clause that does this:
catch(IOException e) {
// Do things, then if you decide you can't handle it...
throw e;
}
All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). If you throw a checked exception from a catch clause, you must also declare that exception! In other words, you must handle and declare, as opposed to handle or declare. The following example is illegal:
public void doStuff() {
try {
// risky IO things
} catch(IOException ex) {
// can't handle it
throw ex; // Can't throw it unless you declare it
}
}
In the preceding code, the dostuff() method is clearly able to throw a checked exception—in this case an IOException—so the compiler says, "Well, that's just peachy that you have a try/catch in there, but it's not good enough. If you might rethrow the IOException you catch, then you must declare it!"
Tell the difficulties that i am difficult.
saloni jhanwar wrote:KB6 Page 377 doubt
I ain't getting this at all.
"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "
Problem is that i had read before this that
(1) If there is any exception encountered in try block then catch block will handle it and at the last finally block will execute
(2) If there is no exception encountered in try block then catch block will be ignored and at last only finally block will execute.
Now in this above statement K&B ,it says if i use finally then all catch blocks will be ignored ? what is this ?
Jesus Angeles wrote:
saloni jhanwar wrote:KB6 Page 377 doubt
I ain't getting this at all.
"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "
Problem is that i had read before this that
(1) If there is any exception encountered in try block then catch block will handle it and at the last finally block will execute
(2) If there is no exception encountered in try block then catch block will be ignored and at last only finally block will execute.
Now in this above statement K&B ,it says if i use finally then all catch blocks will be ignored ? what is this ?
You are analyzing a sentence by itself. Read the topic according to its context. Just a sentence before that, the book says 'you can also throw the same exception you just caught.'. It is talking about rethrowing the same exception you caught. It happens after the normal catch clause catches the first exception thrown.
Tell the difficulties that i am difficult.
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|