曾学过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
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/324170
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!