class Demo{public int show(int index)//该异常可以不抛,原因是该异常不是自己定义的,{int[] arr=new int[3];if (index>=arr.length){throw new ArrayIndexOutOfBoundsException("角标越界了");}return arr[index];}}class ExceptionDemo2{public static void main(String[] args){Demo d=new Demo();try{int num=d.show(3);System.out.println("num="+num);}catch (ArrayIndexOutOfBoundsException e){System.out.println(e.toString());}finally//一定会被执行的代码,一般用于关闭资源,在数据库的关闭中会用到 出现这个,System.exit(0);退出jvm,finally不会被执行到,java虚拟机退出了。{System.out.println("hehe");}System.out.println("over");}}
finally代码块
最新推荐文章于 2023-05-04 14:02:47 发布