资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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;
        }
    }
}
2.自定义异常处理

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

创新互联建站长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为阿合奇企业提供专业的网站制作、网站设计,阿合奇网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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;
    }
}

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
    }
}
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;
    }

}

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());
    }
}
//    父类静态函数块
//    子类静态函数块
//    父类匿名函数块
//    父类无参构造函数
//    子类匿名函数块
//    子类无参构造函数

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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文名称:Java之异常处理-创新互联
标题来源:http://cdkjz.cn/article/dcooco.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220