有种异常叫你没有女朋友

public class NotGirlFriendException extends Exception {
    public NotGirlFriendException() {
    }

    public NotGirlFriendException(String message) {
        super(message);
    }
}
public class GirlFriend {
    String name;
    int age;

    public GirlFriend(String name, int age) throws NotGirlFriendException {
        this();
        this.name = name;
        this.age = age;
    }

    public GirlFriend() throws NotGirlFriendException {
        throw new NotGirlFriendException("you are not girlfriend");
    }
}
public class Test {
    public static void main(String[] args) {
        try {
            GirlFriend girlFriend=new GirlFriend();
        }catch (NotGirlFriendException e){
            System.out.println(e.getMessage());
        }
    }
}

output

you are not girlfriend
发布了51 篇原创文章 · 获赞 52 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_43058685/article/details/104310877
今日推荐