• Java之异常处理


    Java之异常处理

    1.异常处理

    关键词作用备注
    try异常发现区间执行代码逻辑,在执行期间发掘是否有异常
    catch捕获异常后处理区间若try区间有异常,捕获异常在此区间处理
    finally总是执行不论是否有异常 此区间代码总是执行 释放占用资源 如:IO流
    throw抛出方法中异常异常扔到方法定义上(甩锅给上级)
    throws抛出方法定义上异常异常扔到调用方法的地方(甩锅给上级)
    package com.exception;
    // 异常处理五个关键词
    // try 异常发现区间
    // catch 异常处理区间 若try区间有异常,捕获异常在此区间处理
    // finally 不论是否有异常  此区间代码总是执行  释放占用资源 如:IO流
    // throw 抛出异常  方法中  异常扔到方法定义上(甩锅给上级)
    // throws 抛出异常  在方法定义上 异常扔到调用方法的地方(甩锅给上级)
    public class Test01 {
    
    
        public static void main(String[] args) {
            // finally会阻止try catch 的return
    //        System.out.println(new Test01().e2()); // 2
            System.out.println(new Test01().e3()); // 2
    
        }
    
        public void exceptions (int a,int b) throws ArithmeticException{//  此处异常 传递给方法调用第28行 new Test01().exceptions(1,0);
            if(b==0){
                throw new ArithmeticException();//  此处异常 传递给方法定义第18行  public void exceptions (int a,int b) throws ArithmeticException
            }else {
                System.out.println("666");
            }
        }
    
        public void e1(){
            try { // 异常发现区间
                new Test01().exceptions(1,0);
            } catch (ArithmeticException e) { // 捕获异常后处理区间
                e.printStackTrace();
                System.out.println("catch");
            }finally { // 总是执行
                System.out.println("始终执行");
            }
        }
    
        // 测试finally会阻止try catch 的return
        public int e2(){
            try {
                new Test01().exceptions(1,0);
                return 0;
            } catch (ArithmeticException e) {
                e.printStackTrace();
                System.out.println("catch");
                return 1;
            }finally {
                System.out.println("始终执行");
                return 2;
            }
        }
    
    
        // 测试finally会阻止try catch 的return
        public int e3(){
            try {
                new Test01().exceptions(1,1);
                return 0;
            } catch (ArithmeticException e) {
                e.printStackTrace();
                System.out.println("catch");
                return 1;
            }finally {
                System.out.println("始终执行");
                return 2;
            }
        }
    }
    
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    2.自定义异常处理

    MyException类为自定义的异常处理类,Test02类调用异常类
    自定义异常类必须继承Exception类

    MyException类

    package com.exception;
    // 自定义异常类必须继承Exception类
    public class MyException extends Exception{
        private int num;
    
        // MyException构造方法
        public MyException(int num) {
            // 初始化num赋值
            this.num = num;
        }
    
        // 最终异常是什么
        @Override
        public String toString() {
            return "MyException"+this.num;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Test02 类

    package com.exception;
    
    public class Test02 {
        static void e1(int a) throws MyException{  // 此处异常 传递给方法调用第18行  e1(10);
            System.out.println("传入参数:"+a);
    
            if(a<10){
                System.out.println("正常执行");
            }else {
                throw new MyException(a); //此处异常 传递给方法定义第4行 static void e1(int a) throws MyException{
            }
    
            System.out.println("结束方法");
        }
    
        public static void main(String[] args) {
            try {
                e1(10);
            } catch (MyException e) {
                System.out.println("可以添加逻辑处理");
                System.out.println(e); // 打印为MyException类中toString()方法
            }
            // 执行后打印
            // 传入参数:10
            // 可以添加逻辑处理
            // MyException10
        }
    }
    
    
    • 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

    3.扩展之static

    static为静态修饰符,与类一起加载。使用static修饰的方法或者属性,无需实例化, 可以使用类名直接调用。
    探索父类 子类 初始化时 静态函数块 、 匿名函数 、 构造器的运行顺序
    Person父类

    package com.oop.demo06;
    
    public class Person {
        private String name;
        static {
            System.out.println("父类静态函数块");
        }
    
        {
            System.out.println("父类匿名函数块");
        }
    
        public Person(){
            System.out.println("父类无参构造函数");
        }
    
        public Person(String name){
            this.name = name;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Student类为子类

    package com.oop.demo06;
    // 静态导入类
    import static java.lang.Math.random;
    
    public class Student extends Person {
        // 程序运行时只执行一次
        static {
            System.out.println("子类静态函数块");
        }
    
        {
            System.out.println("子类匿名函数块");
        }
    
        public Student(){
            System.out.println("子类无参构造函数");
        }
    
        public static void main(String[] args) {
            Student student = new Student();
    
            // 静态导入类
            System.out.println(random());
        }
    }
    //    父类静态函数块
    //    子类静态函数块
    //    父类匿名函数块
    //    父类无参构造函数
    //    子类匿名函数块
    //    子类无参构造函数
    
    • 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
    • 31

    总目录,请点击此处,Java学习

  • 相关阅读:
    《人类简史》笔记二——一场永远的革命
    开发人员体验:最重要的指标
    分治法思考题
    对Spring的后置处理器BeanPostProcessor的使用
    TC3-Vision应用笔记
    kaist数据集体验
    专访科杰科技CEO于洋:湖仓一体数据底座,企业构建数据能力的“最优解” | 爱分析访谈
    最长回文子串 Golang leecode_5
    5、设计模式之工厂模式
    Django源码学习——配置文件解析
  • 原文地址:https://blog.csdn.net/weixin_45573378/article/details/128088616