Day 36

异常

JVM的默认处理方案

  1. 如果程序出了问题,我们没有做任何处理,最终JVM会做出默认的处理

    • 把程序的名称,异常的原因以及异常出现的位置等信息输出在了控制台
    • 程序停止执行
  2. package demo07;
    
    public class Exception_demo01 {
        public static void main(String[] args) {
            System.out.println("开始");
            run();
            System.out.println("结束");
    
    
        }
    
        public static void run() {
            int[] i = {1, 2, 3, 4};
            //System.out.println(i[1]);  //  2
            System.out.println(i[4]);
        }
    }
    ==============================================
    开始
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    	at demo07.Exception_demo01.run(Exception_demo01.java:15)
    	at demo07.Exception_demo01.main(Exception_demo01.java:6)
    
    Process finished with exit code 1
    

异常处理之try…catch

  1. 格式
        try{
            可能出现的异常的代码;
        } catch(异常类名 变量名){
            异常的处理代码;
        }
    
  2. 执行流程:

    • 程序从try里面的代码开始运行

    • 出现异常,会自动生成一个异常类对象,该异常对象将被提交给JAVA运行时系统

    • 当JAVA运行时系统接收到异常对象时,会到catch中去找匹配的异常类,找到后进行异常的处理,执行完毕后,程序还可以继续运行

  3. package demo07;
    
    public class Exception_demo01 {
        public static void main(String[] args) {
            System.out.println("开始");
            run();
            System.out.println("结束");
    
    
        }
    
        public static void run() {
            int[] i = {1, 2, 3, 4};
            /*
            System.out.println(i[1]);  //  2
             */
            /*
            System.out.println(i[4]);
    
            Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
                at demo07.Exception_demo01.run(Exception_demo01.java:15)
                at demo07.Exception_demo01.main(Exception_demo01.java:6)
             */
            try {
                System.out.println(i[5]);
    
            }catch(ArrayIndexOutOfBoundsException err){
                System.out.println("所寻找的数组索引不存在");
            }
        }
    }
    ===========================================
    开始
    所寻找的数组索引不存在
    结束
    
    Process finished with exit code 0
    

Throwable 的成员方法

  1. 方法名说明
    public String getMassage()返回此throwable的详细消息字符串
    public String toString()返回此可抛出的简短描述
    public void printStackTrace把异常的错误信息输出在控制台
  2. package demo07;
    
    public class Exception_demo01 {
        public static void main(String[] args) {
            System.out.println("开始");
            run();
            System.out.println("结束");
        }
        public static void run() {
            int[] i = {1, 2, 3, 4};
            try {
                System.out.println(i[5]);
                    /*
            Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
                at demo07.Exception_demo01.run(Exception_demo01.java:15)
                at demo07.Exception_demo01.main(Exception_demo01.java:6)
             */
    
            }catch(ArrayIndexOutOfBoundsException err){
                System.out.println("所寻找的数组索引不存在");
                err.printStackTrace();               // 打印发生异常的位置详细信息
            }
        }
    }
    ====================================
    开始
    所寻找的数组索引不存在
    结束
    java.lang.ArrayIndexOutOfBoundsException: 5
    	at demo07.Exception_demo01.run(Exception_demo01.java:14)
    	at demo07.Exception_demo01.main(Exception_demo01.java:6)
    
    Process finished with exit code 0
    

编译时异常和运行时异常的区别

  1. java中的异常被分为两大类:编译时异常和运行时异常,也别成为受检异常和非受检异常,所有的RuntimeException类及其子类被称为运行时异常,其他的异常都是编译时异常

    • 编译时异常:必须显示处理,在编译时就是提示异常,不做修改程序就无法进行编译。
    • 运行时异常:无需显示处理,程序在运行时才会发现的异常。
  2. package demo07;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Exception_demo02 {
        public static void main(String[] args) {
            method();
            System.out.println("------------");
            method_02();
            System.out.println("------------");
            method_03();
        }
    
        public static void method() {   //  运行时异常
            try {
                int[] arr = {1, 2, 3, 4};
                System.out.println(arr[4]);
            /*
            ArrayIndexOutOfBoundsException: 4
             */
            } catch (ArrayIndexOutOfBoundsException err) {
                err.printStackTrace();
            }
        }
    
        public static void method_02() {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String ss = sdf.format(d);
            System.out.println(ss);    //     2022年03月18日 17:11:37
        }
    
        public static void method_03() {
            try {
                String s = "2022-3-18";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(s);
                System.out.println(d);  //    编译时异常--->抓取、抛出异常
                                        //   Fri Mar 18 00:00:00 CST 2022
            } catch (ParseException err) {
                err.printStackTrace();
            }
        }
    }
    ===================================
    java.lang.ArrayIndexOutOfBoundsException: 4
    	at demo07.Exception_demo02.method(Exception_demo02.java:19)
    	at demo07.Exception_demo02.main(Exception_demo02.java:9)
    ------------
    2022031817:11:37
    ------------
    Fri Mar 18 00:00:00 CST 2022
    
    Process finished with exit code 0
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值