|NO.Z.00070|——————————|BigDataEnd|——|Java&集合类库.V06|——|Java.v06|异常机制.v06|自定义异常|使用|

一、自定义异常
### --- 基本概念

——>        当需要在程序中表达年龄不合理的情况时,而Java官方又没有提供这种针对性的异常,
——>        此时就需要程序员自定义异常加以描述。
### --- 实现流程

——>        a.自定义xxxException异常类继承Exception类或者其子类。
——>        b.提供两个版本的构造方法,一个是无参构造方法,另外一个是字符串作为参数的构造方法。
### --- 异常的产生

——>        throw new 异常类型(实参);
——>        如:
——>        throw new AgeException("年龄不合理!!!");
——>        Java采用的异常处理机制是将异常处理的程序代码集中在一起,
——>        与正常的程序代码分开,使得程序简洁、优雅,并易于维护。
二、编程代码
package com.yanqi.task16;

public class AgeException extends Exception {

    static final long serialVersionUID = 7818375828146090155L; // 序列化的版本号  与序列化操作有关系

    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }
}
三、编程代码
package com.yanqi.task16;

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) /*throws AgeException */{
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) /*throws AgeException */{
        if (age > 0 && age < 150) {
            this.age = age;
        } else {
            //System.out.println("年龄不合理哦!!!");
            try {
                throw new AgeException("年龄不合理哦!!!");
            } catch (AgeException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
四、编程代码
package com.yanqi.task16;

public class PersonTest {

    public static void main(String[] args) {

        /*Person p1 = null;
        try {
            p1 = new Person("zhangfei", -30);
        } catch (AgeException e) {
            e.printStackTrace();
        }
        System.out.println("p1 = " + p1); // zhangfei 0*/
        Person p1 = new Person("zhangfei", -30);
        System.out.println("p1 = " + p1); // zhangfei 0  null
    }
}
五、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54908:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task16.PersonTest
com.yanqi.task16.AgeException: 年龄不合理哦!!!
    at com.yanqi.task16.Person.setAge(Person.java:33)
    at com.yanqi.task16.Person.<init>(Person.java:12)
    at com.yanqi.task16.PersonTest.main(PersonTest.java:14)
p1 = Person{name='zhangfei', age=0}

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(22)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示