异常处理
异常的处理
Java异常处理的
五个关键字:try、catch、fifinally、throw、throws
抛出异常throw
在编写程序时,我们必须要考虑程序出现问题的情况。比如,在定义方法时,方法需要接受参数。那么,当调用方法使用
接受到的参数时,首先需要先对参数数据进行合法的判断,数据若不合法,就应该告诉调用者,传递合法的数据进来。这时需要使用抛出异常的方式来告诉调用者。
作用:
使用格式:
注意:
代码举例package demo01; public class Demo03Throw { public static void main(String[] args) { //int[] arr = null; int[] arr = new int[3]; int e = getElement(arr,3); System.out.println(e); } /* 定义一个方法,获取数组指定索引处的元素 参数: int[] arr int index 以后(工作中)我们首先必须对方法传递过来的参数进行合法性校验 如果参数不合法,那么我们就必须使用抛出异常的方式,告知方法的调用者,传递的参数有问题 注意: NullPointerException是一个运行期异常,我们不用处理,默认交给JVM处理 ArrayIndexOutOfBoundsException是一个运行期异常,默认交给JVM处理 */ public static int getElement(int[] arr,int index){ /* 我们可以对传递过来的参数数组,进行合法性校验 如果数组arr的值是null 那么我们就抛出空指针异常,告知方法的调用者"传递的数组的值是null" */ if(arr == null){ throw new NullPointerException("传递的数组的值是null"); } /* 我们可以对传递过来的参数index进行合法性校验 如果index的范围不在数组的索引范围内 那么我们就抛出数组索引越界异常,告知方法的调用者"传递的索引超出了数组的使用范围" */ if(index<0 || index>arr.length-1){ throw new ArrayIndexOutOfBoundsException("传递的索引超出了数组的使用范围"); } int ele = arr[index]; return ele; } } Objects非空判断要判断一个对象是否为空,可以使用Objects类中的方法,简化代码书写。
代码举例 public static void method(Object obj){ //对传递过来的参数进行合法性判断,判断是否为null /*if(obj == null){ throw new NullPointerException("传递的对象的值是null"); }*/ //Objects.requireNonNull(obj); Objects.requireNonNull(obj,"传递的对象的值是null"); } throws关键字:异常处理的第一种方式,交给别人处理作用:
使用格式:在方法声明时使用
throw new AAAExcepiton("产生原因");
try...catch:异常处理的第二种方式,自己处理异常 格式: }
如何获取异常信息:
Throwable类(子类都可以使用)中定义了一些查看方法:
包含了异常的类型,异常的原因,还包括异常出现的位置,在开发和调试阶段,都得使用printStackTrace。
代码举例package demo02; import java.io.IOException; public class Demo01TryCatch { public static void main(String[] args) { try{ //可能产生异常的代码 readFile("d:a.tx"); System.out.println("资源释放"); }catch (IOException e){//try中抛出什么异常对象,catch就定义什么异常变量,用来接收这个异常对象 //异常的处理逻辑,怎么处理异常对象 //System.out.println("catch - 传递的文件后缀不是.txt"); /* Throwable类中定义了3个异常处理的方法 String getMessage() 返回此 throwable 的简短描述。 String toString() 返回此 throwable 的详细消息字符串。 void printStackTrace() JVM打印异常对象,默认此方法,打印的异常信息是最全面的 */ //System.out.println(e.getMessage());//文件的后缀名不对 //System.out.println(e.toString());//重写Object类的toString java.io.IOException: 文件的后缀名不对 //System.out.println(e);//java.io.IOException: 文件的后缀名不对 /* java.io.IOException: 文件的后缀名不对 at com.itheima.demo02.Exception.Demo01TryCatch.readFile(Demo01TryCatch.java:55) at com.itheima.demo02.Exception.Demo01TryCatch.main(Demo01TryCatch.java:27) */ e.printStackTrace(); } System.out.println("后续代码"); } /* 如果传递的路径,不是.txt结尾 那么我们就抛出IO异常对象,文件的后缀名不对 */ public static void readFile(String fileName) throws IOException { if(!fileName.endsWith(".txt")){ throw new IOException("文件的后缀名不对"); } System.out.println("路径没有问题,读取文件"); } } finally代码块 格式: }finally{
代码举例public class Demo02TryCatchFinally { public static void main(String[] args) { try { //可能会产生异常的代码 readFile("c:a.tx"); } catch (IOException e) { //异常的处理逻辑 e.printStackTrace(); } finally { //无论是否出现异常,都会执行 System.out.println("资源释放"); } } /* 如果传递的路径,读取文件"); } } 异常注意事项
多个异常使用捕获又该如何处理呢?
一般我们是使用一次捕获多次处理方式,
格式如下:
代码举例package demo03; import java.util.List; /* 异常的注意事项 */ public class Demo01Exception { public static void main(String[] args) { /* 多个异常使用捕获又该如何处理呢? 1. 多个异常分别处理。 2. 多个异常一次捕获,多次处理。 3. 多个异常一次捕获一次处理。 */ //1. 多个异常分别处理。 /* try { int[] arr = {1,2,3}; System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3 }catch (ArrayIndexOutOfBoundsException e){ System.out.println(e); } try{ List<Integer> list = List.of(1,3); System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3 }catch (IndexOutOfBoundsException e){ System.out.println(e); }*/ //2. 多个异常一次捕获,多次处理。 /*try { int[] arr = {1,3}; //System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3 List<Integer> list = List.of(1,3); System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3 }catch (ArrayIndexOutOfBoundsException e){ System.out.println(e); }catch (IndexOutOfBoundsException e){ System.out.println(e); }*/ /* 一个try多个catch注意事项: catch里边定义的异常变量,如果有子父类关系,那么子类的异常变量必须写在上边,否则就会报错 ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException */ /*try { int[] arr = {1,3); System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3 }catch (IndexOutOfBoundsException e){ System.out.println(e); }catch (ArrayIndexOutOfBoundsException e){ System.out.println(e); }*/ //3. 多个异常一次捕获一次处理。 /*try { int[] arr = {1,3); System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3 }catch (Exception e){ System.out.println(e); }*/ //运行时异常被抛出可以不处理。即不捕获也不声明抛出。 //默认给虚拟机处理,终止程序,什么时候不抛出运行时异常了,在来继续执行程序 int[] arr = {1,3}; System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3 List<Integer> list = List.of(1,3); System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3 System.out.println("后续代码!"); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |