java——自定义异常

2019年2月17日14:20:55 发表评论 1,038 views
class defaultep {
    static class DefaultException extends Exception {
        public DefaultException(String msg) {
            //调用Exception类的构造方法,存入异常信息
            super(msg);
        }
    }

    public static void main(String args[]) {
        try {
            //这里用throw直接抛出一个DefaultException类的实例对象
            throw new DefaultException("自定义异常!");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: