当前位置: 移动技术网 > IT编程>开发语言>Java > GirlFriend NotFound Exception(七夕情人节限定款)

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

2020年08月01日  | 移动技术网IT编程  | 我要评论
首先我们需要实现一个自己的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();
        }
    }
}

来看看结果

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



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

终究还是一个人扛下了所有

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网