Questions:
1. Is it a ok practice to, shall we say, abuse try/catch nested blocks in this fashion?
2. As in other nested blocks (i.e. if-else if), just looking at this code, I have to ask, how many levels, before is time to redesign the code?
Code: Select all
int[] ia = new int[]{ 1, 2, 3};
try{
System.out.println(ia[-1]); //ArrayIndexOutOfBoundsException
}catch(ArrayIndexOutOfBoundsException ai1){
System.out.println(ai1);
try{
System.out.println(ia[3]); //ArrayIndexOutOfBoundsException
}catch(ArrayIndexOutOfBoundsException ai3){
System.out.println(ai1);
try{
System.out.println("0123".charAt(4)); //StringIndexOutOfBoundsException
}catch(StringIndexOutOfBoundsException si){
System.out.println(si);
}
}
}