# exception_xx #
package 异常处理;
public class exception_xx{
public static void func(){
for(int i=0;i<3;i++){
int k;
try{
System.out.println("exception handling . ...");
try{
switch(i){
case 0:
int zero = 0;
k = 6 / zero;
System.out.println(k);
break;
case 1:
int []Array = new int[2];
k = Array[9];
System.out.println(k);
break;
case 2:
char ch = "Hello".charAt(66);
System.out.println(ch);
break;
}
}
catch(ArithmeticException e){
System.out.println(" ArithmeticException " + e.getMessage());
}
catch(IndexOutOfBoundsException e){
System.out.println(" IndexOutOfBoundsException " + e.getMessage());
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(" ArrayIndexOutOfBoundsException " + e.getMessage());
}
}
}
}
# exception_xxtest #
package 异常处理;
public class exception_xxtest {
public static void main(String[] args) {
exception_xx xxx = new exception_xx();
try{
xxx.func();
}
catch(RuntimeException e){
System.out.println(" main RuntimeException " + e.getMessage());
}
}
}
//测试结果:
/********************************************************
* ******************************************************
exception handling . ...
ArithmeticException / by zero
exception handling . ...
IndexOutOfBoundsException 9
exception handling . ...
IndexOutOfBoundsException String index out of range: 66
********************************************************
* ******************************************************/