Reputation: 129
I have the following scenario.
public void foo() throws BadFooException, IOException{
try{
..........
} catch(ArrayIndexOutOfBoundException e) {
throw new BadFooException(e);
} catch(ArithmeticException e) {
throw new BadFooException(e);
}
}
Here BadFooException
is a class that extends Exception
. But to my desire BadFooException
shall only catch
ArrayIndexOutOfBoundException
and
ArithmeticException
not the other exceptions. I am always catching those two exception and throwing my desired exception. But is there any way, so that my desired exception will be thrown automatically in case of only these two exceptions but other exceptions will be thrown separately? Thanks in advance.
Upvotes: 0
Views: 257
Reputation: 476567
It's quite clear what you ask... However if you want to combine several exceptions, to save some typing work, you could use:
public void foo() throws CustomException1, OtherException {
try {
// stuff (that can throw Exception1, Exception2 and OtherException)
} catch (Exception1 | Exception2 ex) {
throw new CustomException1(ex);
}
}
Thus with a vertical bar in between.
As far as I know, there is no more elegant way to do this.
Upvotes: 1