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

2022-07-30,,,

首先我们需要实现一个自己的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();
        }
    }
}

来看看结果

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


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

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

《GirlFriend NotFound Exception(七夕情人节限定款).doc》

下载本文的Word格式文档,以方便收藏与打印。