[HD Pg 292, Sec. 10.4.0 - common-exceptions-that-are-usually-thrown-by-the-jvmexceptions-thrown-by-jvm]
Posted: Fri Mar 15, 2019 6:03 pm
I rewrote the code in the section as nested try blocks.
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?
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);
}
}
}