欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GirlFriend NotFound Exception(七夕情人节限定款)

程序员文章站 2022-03-26 16:33:12
首先我们需要实现一个自己的Exception/** * GirlFriendNotFoundException * @author Dongyu ZHAO */public class GirlFriendNotFoundException extends Exception { private String msg; public GirlFriendNotFoundException(String msg) { this.msg = msg; }...

首先我们需要实现一个自己的Exception

/**
 * GirlFriendNotFoundException
 * @author Dongyu ZHAO
 */
public class GirlFriendNotFoundException extends Exception {
    private String msg;

    public GirlFriendNotFoundException(String msg) {
        this.msg = msg;
    }

    @Override
    public String getMessage() {
        return msg;
    }
}

然后来定义下女朋友这个实体

public class GrilFriend {
    private String name;
    private String age;
    private String desc;

    public GrilFriend(String name, String age, String desc) {
        this.name = name;
        this.age = age;
        this.desc = desc;
    }
}

下面来定义下抛异常的约会场景

public class DatingGirlFriend {
    void date(List<GirlFriend> list) throws GirlFriendNotFoundException {
        if(list.size()==0){
            throw new GirlFriendNotFoundException("醒醒,你没有女朋友!!!");
        }
    }
}

来看看七夕节发生了什么。。

public class Qixi {
    public static void main(String[] args) {
        List<GirlFriend> myGirls=new ArrayList<>();
        DatingGirlFriend me = new DatingGirlFriend();
        try {
            //尝试找一位女朋友约会。。。
            me.date(myGirls);
        } catch (GirlFriendNotFoundException e) {
            e.printStackTrace();
        }
    }
}

来看看结果

GirlFriend NotFound Exception(七夕情人节限定款)

所以结论就是自己new一个GirlFriend就不会抛异常啦,大家学会了吗?【手动狗头】



顺便帮自己许愿一个,或许明年就不抛异常了呢。。。

GirlFriend NotFound Exception(七夕情人节限定款)

本文地址:https://blog.csdn.net/qq_38905818/article/details/108225514

相关标签: NotFound Exception