java中如何创建自定义异常Create Custom Exception

来源:互联网 发布:纽约 攻略 知乎 编辑:程序博客网 时间:2024/06/13 08:47
9.创建自定义异常 Create Custom Exception 马克-to-win:我们可以创建自己的异常:checked或unchecked异常都可以, 规则如前面我们所介绍,到底哪个好,笔者认为根据个人喜好,只要按语法即可。这两种选择,各路架构师大神的意见是50对50。笔者不作评判。

例1.9.1
public class Test {
    public static void main(String args[]) throws RelationshipExceptionMark_to_win {
        int talkTimesPerDay = 2;
        if (talkTimesPerDay < 3) {
            RelationshipExceptionMark_to_win e = new RelationshipExceptionMark_to_win();
            e.setMsg("每天说话小于3 次,抛出关系异常的异常,分手");
            throw e;
        }
        System.out.println("马克-to-win:优雅结束");
    }
}
class RelationshipExceptionMark_to_win extends Exception {
    String msg;
    String getMsg() {
        return msg;
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner5_web.html#CreateCustomException
0 0
原创粉丝点击