-
如何自定义异常类
-
继承现有的异常结构:RuntimeException(运行时异常)、Exception
-
提供全局常量:serialVersionUID
-
提供多个重载构造方法
package October_13; public class test2 extends RuntimeException { static final long serialVersionUID = -7034897190745766939L; public test2() { } public test2(String msg) { super(msg); } }
-
-
如何手动抛出异常
- throw new 异常类名
package October_13; public class test { public static void main(String[] args) { t t = new t(); t.re(-1010); System.out.println(t); } } class t { private int id; public void re(int id) { if (id > 0) { this.id = id; } else { throw new test2("调用test2自定义异常"); } } @Override public String toString() { return "t{" + "id=" + id + '}'; } }