class MyException extends Exception {
public MyException(){}
public MyException(String msg){
super();
}
}
public class FullConstructors{
public static void f() throws MyException{
System.out.println("Throwing MyException from f()");
throw new MyException();
}
public static void g() throws MyException{
System.out.println("Throwing MyException from g()");
throw new MyException("Originated in g()");
}
public static void main(String[] args){
try{
f();
}catch(MyException e){
e.printStackTrace(System.out);
}
try{
g();
}catch(MyException e){
e.printStackTrace(System.out);
}
}
}
两个构造器定义了MyException类型对象的创建方式,对于第二个构造器,使用super关键字明确调用了其基类构造器,他接受一个字符串作为参数。
异常处理程序中,调用了Throwable类声明的printStackTrace()方法。