try,catch,finally尝试(一个程序块多个catch)


曾学过c++,但是对这些异常捕捉不是很了解,通过别的编程语言了解

public class newclass {

public static void main(String[] args) {
try {int [] arr=new int[5];
arr[1]=0;
//    arr[10]=1;
arr[0]=3;
int res=arr[0]/arr[1];
}
catch(ArrayIndexOutOfBoundsException ex2) {
System.out.println("2");

ex2.printStackTrace(); }
catch(ArithmeticException ex1){
System.out.println("1");

ex1.printStackTrace(); 

}
catch(Exception ex){ex.printStackTrace(); } 
finally{
System.out.println("不存在的");}

System.out.println("为什么急着");}

}

总结

一个程序块多个catch只会处理符合的那个,其他自定义的catch异常不会处理,而数组越界是编译器最早发现的,所以最早抛出错误,所以是2.

 

posted @ 2020-01-09 15:38  世界を変える御宅族  阅读(501)  评论(0编辑  收藏  举报