• 19异常的学习笔记


    异常

    很重要,有利于我们平时处理问题
    异常就是代表程序出现了问题
    常见的异常比如说

    1. 数组越界
    2. 除法除0

    异常的体系是什么

    java.lang.Throwable
    Error Exception
    RuntimeException 其他异常

    Error 代表的是系统级别的错误,也就是一旦系统出现问题,sun公司会把这些问题封装程Error对象出来
    Error 是sun公司自己用的,不是给我们程序员用的,我们开发人员不用管
    Exception:叫异常,它代表的才是我们程序可能出现的问题,所以,我们程序员通常会用 Exception 以及它的孩子来封装程序出现的问题。
    运行时异常:RuntimeException及其子类,编译阶段不会出现错误提醒,运行时出现的异常(如:数组索引越界异常)
    编译时异常:编译阶段就会出现错误提醒的。(如:日期解析异常)
    抓住异常,我们通过这个代码来进行抓住异常,如果 try 里面的代码是有异常的,那我们就进行捕捉,如果捕捉到,我们就可以得到这个异常信息,并输出这个异常的信息

            try {
                SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date d = date.parse("2020-11-5 11:50:90");
                System.out.println(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    自定义运行时异常

    1. 定义一个异常类继承RuntimeExceptin
    2. 重写构造器
    3. 通过throw new 异常类来创建异常对象并输出

    编译阶段不报错,提醒不强烈,运行时才报错

    public class exception {
        public static void main(String[] args) {
                saveAge(180);
        }
        public  static  void saveAge(int age){
            if(age>0&&age<150){
                System.out.println("年龄被成功保存"+age);
            }else {
                throw  new AgeIllegalRuntimeException("age is illegal ,your age is "+age);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    我们这个新建的这个异常类我们是继承这个RuntimeException

    public class AgeIllegalRuntimeException extends RuntimeException{
    	//构造函数
        public AgeIllegalRuntimeException() {
        }
    	//构造函数,其中这个message是输出的异常信息
        public AgeIllegalRuntimeException(String message) {
            super(message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    抛出编译时异常

    public class exception {
        public static void main(String[] args) {
                saveAge(12);
                //saveAge2(25);   //这里会直接报错,我们有两种解决办法,一种是继续往上抛
                try {
                    saveAge2(160);
                    System.out.println("程序正常执行");
                } catch (AgeIllegleException e) {
                    System.out.println("程序异常");
                    throw new RuntimeException(e);
                }
    
        }
        public  static  void saveAge(int age){
            if(age>0&&age<150){
                System.out.println("年龄被成功保存"+age);
            }else {
                throw  new AgeIllegalRuntimeException("age is illegal ,your age is "+age);
            }
        }
        //throws 是在这个方法中抛出异常,让该方法调用的时候出现
        //throw 跑出去一个异常对象。
        public  static  void saveAge2(int age) throws AgeIllegleException {
            if(age>0&&age<150){
                System.out.println("年龄被成功保存"+age);
            }else {
                throw  new AgeIllegleException("age is illegal ,your age is "+age);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    分析上面的代码,我们可以看到这些异常的处理的时候我们发现我们可以不断的向外面抛出异常,但是一直抛出异常肯定是不可以的,因此我们需要对其进行捕获异常,利用try catch,进行记录异常,并记录处理信息

    异常处理

    1. 捕获异常,记录异常并相应合适的信息给用户
    2. 捕获异常,尝试重新修复
    
    • 1
    • 2

    1. 捕获异常,记录异常并相应合适的信息给用户

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class ExceptionNesting {
        public static void main(String[] args) {
            try {
                test1();
            } catch (FileNotFoundException e) {
                System.out.println("文件未找到");
                throw new RuntimeException(e);
            } catch (ParseException e) {
                System.out.println("日期格式 不正确");
                throw new RuntimeException(e);
            }
        }
        public static void test1() throws FileNotFoundException, ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse("2020-10-20 12:12");
            System.out.println(date);
            test2();
        }
        public static void test2() throws FileNotFoundException {
            InputStream is = new FileInputStream("D:/meinv.png");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2. 捕获异常并进行处理

    import java.util.Scanner;
    
    public class StrongExceptionCorrect {
        public static void main(String[] args) {
            while (true) {
                try {
                    System.out.println(getMoney());
                    break;
                } catch (Exception e) {
                    System.out.println("你输入的价格不合适请重新输入(你输入的并不是一个double类型的变量,可能输入了字符串导致异常)");
                }
            }
        }
        public static double getMoney(){
            Scanner input = new Scanner(System.in);
            while (true){
                System.out.println("请您输入合适的价格");
                double money = input.nextInt();
                if(money>=0){
                    return money;
                }else {
                    System.out.println("你输入的价格是不合适的");
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    这个零代码神器,业务不懂技术也能用,大大降低IT人员的报表需求
    通过百度翻译API完成Java中的中英文翻译
    PyTorch 与 TensorFlow:机器学习框架之战
    【面试题精讲】Mysql如何实现乐观锁
    JTabbedPane 点 x 关闭选项卡
    计算机毕业设计django基于python精品课程在线学习系统(源码+系统+mysql数据库+Lw文档)
    【哈佛公开课】积极心理学笔记-06乐观主义(上)
    【C++11保姆级教程】继承构造函数和花括号等式初始化器(brace-or-equal initializers)
    XBanner源码详解
    Qi标准无线供电模块如何处理噪声抑制语音通讯接收灵敏度
  • 原文地址:https://blog.csdn.net/everything_study/article/details/133105303