Java--异常--知识总结
异常的引出
-
package com.model.yichang; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/2 22:40 * 异常的概念 * 异常体系图 * 常见的异常 * 异常处理 * 自定义异常 * throw和throws的对比 */ public class YiChangDemo01 { public static void main(String[] args) { // 如果我们不捕获异常,当出现异常时程序就无法继续向下执行了,导致了整个系统的健壮性很差, // 为了系统能继续执行,我们可以对可能发生异常的语句进行捕获抛出异常, // 当程序即使出现了异常也不会中断,也可以继续执行,不影响程序的整体执行 // 如果我们对异常不进行捕获,当发生错误的时候会造成程序中断执行 // 快捷键:ctrl+Alt+t // 异常处理的价值:让程序不会直接崩溃,而是继续执行,增加系统的健壮性 int num1=10; int num2=0; try { int res=num1/num2; } catch (Exception e) { e.printStackTrace(); }finally { System.out.println("继续执行"); } System.out.println("继续执行"); } }
-
异常的基本介绍
-
异常的体系图(重点) :
-
运行时异常:一般是我们代码的逻辑错误,程序应该避免的一种错误,编译器无法检测到,不需要我们处理的异常
-
编译时异常: 编译器能检测到的,我们需要进行处理的异常
-
-
五大运行时异常
-
package com.model.exception; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 12:12 * 演示五大运行时异常 */ public class ExceptionDemo03 { public static void main(String[] args) { // 1.空指针异常 try { String str=null; System.out.println(str.length());//NullPointerException } catch (Exception e) { e.printStackTrace(); } // 2.数组下标越界异常 try { String[] arr={"a","b","c"}; for (int i=0;i<=arr.length;i++){ System.out.println(arr[i]);//.ArrayIndexOutOfBoundsException } } catch (Exception e) { e.printStackTrace(); } // 3.数字格式异常 try { String str01="张紫韩"; int num=Integer.parseInt(str01);//NumberFormatException } catch (NumberFormatException e) { e.printStackTrace(); } // 4.算数异常 try { int num02=10/0;//ArithmeticException } catch (Exception e) { e.printStackTrace(); } // 5.类型转换异常 try { A b = new B(); B b1 = (B) b;//ok C b2 = (C) b; //ClassCastException } catch (Exception e) { e.printStackTrace(); } } } class A{} class B extends A{} class C extends A{}
-
常见的编译异常:
-
异常处理的两种方式:
-
若我们对程序不进行try-catch处理默认的,则默认会有一个throws处理,当发生异常时会交给虚拟机进行处理,虚拟机直接将错误的信息输出,且终端程序;如果我们对程序进行了处理则不会使用throws进行处理
-
try-catch时我们手动的进行处理的操做,不会终端程序;throws时系统默认的处理方式,会中断程序
-
try-catch使用语法细节:
-
package com.model.exception; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 16:15 * 演示try-catch的使用细节 */ public class ExceptionDemo04 { public static void main(String[] args) { // 1.在try语句中如果发生异常,下面的语句就不会执行,而是直接到了catch中,但是catch后面的语句会继续执行 // 2.如果没有发生异常,则直接进入到finally中执行,不会执行catch中信息 // 3.finally:不管程序是否抛出异常都会执行的,(关闭连接,释放资源)使用的是finally语句 // 4.可以使用多个catch捕获不同的异常 // 5.存在try-finally:这种情况没有对程序进行处理,还是虚拟机进行处理,会抛出异常终端程序,但是finally中的语句还是会执行 try { System.out.println("开始执行"); String str="张紫韩"; int num=Integer.parseInt(str); // System.out.println("执行完毕"); int num1=10/0; } catch (NumberFormatException e) { e.printStackTrace();//在程序的最后面抛出异常 System.out.println(e.getMessage());//按执行顺序执行在程序的中间抛出信息 }catch (ArithmeticException e){ e.printStackTrace(); System.out.println(e.getMessage()); }finally{ System.out.println("finally执行"); } System.out.println("程序结束"); } }
-
package com.model.homework; import java.util.Scanner; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 16:59 * 提示用户输入数据,如果他输入的不是整数,就一直输入 */ public class HomeWorkDemo01 { public static void main(String[] args) { int num = 0; Scanner scanner = new Scanner(System.in); boolean loop=true; System.out.println("请输入一个整数"); while(loop) { try { String next = scanner.next(); num = Integer.parseInt(next); loop=false; } catch (NumberFormatException e) { System.out.println("请输入一个整数"); } } System.out.println("输入正确"+num); } }
-
throws异常处理
-
package com.model.exception; import java.io.FileNotFoundException; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 18:57 * 演示 Throws的使用细节 */ public class ExceptionDemo08 { public static void main(String[] args) { } public static void One() throws FileNotFoundException { Two(); //当调用的方法抛出编译时异常是,我们也必须进行处理 } public static void Two() throws FileNotFoundException { //编译时异常 } public static void Three() /*throws RuntimeException */{ Four(); //当调用的方法抛出运行时异常是,我们可以不对此进行处理,因为对于运行时异常我们由默认的处理机制 :即默认的抛出异常 //而对于编译时异常我们则没有默认的处理,必须手动的处理 // 对于程序员来说,运行时异常可以不进行处理(有默认的处理机制);但是编译时异常我们就必须手动的进行处理了 } public static void Four() throws RuntimeException{ //抛出运行时异常 } }
-
自定义的异常:
-
package com.model.customexception; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 19:08 * 自定义的异常类 */ public class CustomExceptionClass01 { public static void main(String[] args) { int age=10; if (age>120||age<20){ throw new AgeException("年龄不在范围内"); } } } /** * 1.一般情况我们的自定义异常都是继承RunTimeException * 2.我们把自定义异常做成运行时异常的好处,我们可以使用默认的处理机制,不用手动的抛出处理异常了 * 3.方便 * * */ //自定义的一个异常 class AgeException extends RuntimeException{ //这里我们可以通过构造器,设置提示信息 public AgeException(String message) { //自定义的异常类 super(message); } }
-
throw和throws的区别:
-
练习题:
-
package com.model.homework; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 19:31 * 演示finally的执行顺序 */ public class HomeWorkDemo03 { public static void main(String[] args) { //异常信息的抛出在这个程序的最后抛出 System.out.println(one()); two(); System.out.println(three()); } // 1.try里面有return i++; 时,finally在return之前 i++之后执行 public static int one(){ int i=0; try { return i++; }catch (Exception e){ e.printStackTrace(); }finally { System.out.println("在return之前执行"+i); } return i; } // 1 2.try里面有抛出异常的操做时,finally在抛出异常的操作只有执行 public static void two(){ try { throw new RuntimeException(); }catch (Exception e){ e.printStackTrace(); }finally { System.out.println("在异常信息之后执行"); } } // 3.try里面有return i; 时,finally中对i进行操做,返回的不会变,是因为try中保留了副本 public static int three(){ int i=0; try { return i; //输出0 ,保存了副本为0,则最后返回为0,finally对i进行操做不会影响这里的返回值 }catch (Exception e){ e.printStackTrace(); }finally { i++; System.out.println("finally对try的返回值进行操做,不会影向try里面的返回值"); } return i; } }
-
-
package com.model.homework; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/3 21:32 */ public class HomeWorkDemo04 { public static void main(String[] args) { try { if (args.length!=2){ throw new ArrayIndexOutOfBoundsException("参数个数不正确"); } int num1=Integer.parseInt(args[0]); int num2=Integer.parseInt(args[1]); int cal = cal(num1, num2); System.out.println("结果是:"+cal); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } catch (NumberFormatException e) { System.out.println(e.getMessage()); }catch (ArithmeticException e){ System.out.println(e.getMessage()); } } public static int cal(int n1,int n2){ return n1/n2; } }
-
-