曾学过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.

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/otakus/p/12171808.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程