Catch the same exception twice
I have the following:
public void method(){
try {
methodThrowingIllegalArgumentException();
return;
} catch (IllegalArgumentException e) {
anotherMethodThrowingIllegalArgumentException();
return;
} catch (IllegalArgumentException eee){ //1
//do some
return;
} catch (SomeAnotherException ee) {
return;
}
}
Java doesn't allow us to throw the exception twice, which is why we got a compilation error in //1
. But I need to do exactly what I am trying to do:
try the method first methodThrowingIllegalArgumentException()
, and if it doesn't work with IAE
, try anotherMethodThrowingIllegalArgumentException();
, if it fails with IAE
, do some operations and return. If it works with SomeAnotherException
, just return.
How can i do this?
source to share
If a call anotherMethodThrowingIllegalArgumentException()
inside a block catch
can throw an exception, it should be caught there, not as part of a "top-level" statement try
:
public void method(){
try{
methodThrowingIllegalArgumentException();
return;
catch (IllegalArgumentException e) {
try {
anotherMethodThrowingIllegalArgumentException();
return;
} catch(IllegalArgumentException eee){
//do some
return;
}
} catch (SomeAnotherException ee){
return;
}
}
source to share