class Demo{
public static void main(String args[]){
int x=3,n=5,d=0;
int ar[]=new int[3];
String name="Neno";
System.out.println("Start main");
try{
ar[x]=name.charAt(n)/d; //n=5
}catch(StringIndexOutOfBoundsException e){
System.out.println("String index Error");
}catch(RuntimeException e){
System.out.println("Any runtime Error");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index Error");
}catch(ArithmeticException e){
System.out.println("Arithmetic Error");
}
System.out.println("End main");
}
}
I used this code to filter some exceptions,but there is an error in the code. It says to remove the catch-clauses of ArrayIndexOutOfBounds
and ArithmeticException
. Is it because the order of the catch-clauses the error springs up? When I change the order like this...
class Demo{
public static void main(String args[]){
int x=3,n=5,d=0;
int ar[]=new int[3];
String name="Niroth";
System.out.println("Start main");
try{
ar[x]=name.charAt(n)/d; //n=5
}catch(StringIndexOutOfBoundsException e){
System.out.println("String index Error");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index Error");
}catch(ArithmeticException e){
System.out.println("Arithmetic Error");
}catch(RuntimeException e){
System.out.println("Any runtime Error");
}catch(Exception e){
System.out.println("Any Error");
}
System.out.println("End main");
}
}
There was no error in this order. Can anyone explain me the reason for this issue?