Reputation: 475
What happens if I try to catch both an Exception and say an InterruptedException like this:
try {
someCode();
} catch (Exception e) {
doSomething();
} catch (InterruptedException e) {
specialTreatmentForThisException();
}
In this case InterruptedException extends Exception. What happens if an InterruptedException is thrown? Will it go to that catch-clause and for all other Exceptions the first clause?
The desired outcome is to handle one exception thrown in a special way, and all the rest of the exceptions in one way.
Thanks and best regards.
Upvotes: -3
Views: 1711
Reputation: 548
Exception is parent therefore children InterruptedException class is not used. Change parent position and children position.
Upvotes: 0
Reputation: 724
As already added by Thomas and User you have to create catch blocks of specific exceptions first, and at the last, you have to use the general exception. If you use the general exception first then the specific exceptions would be unreachable and the compiler will throw an error saying
error: exception InterruptedException has already been caught catch(InterruptedException e){
try {
---
---
}
catch(InterruptedException e) {
specialTreatmentForThisException();
}
catch(Exception e){
doStuff();
}
Upvotes: 0