class MyException extends Exception {
String msg;
public MyException(String msg) {
this.msg = msg;
}
public void printMsg() {
System.out.println("msg = " + msg);
}
}
// Or you can take a more clever approach, and
// note that string storage and printing is
// built into Exception:
class MyException2 extends Exception {
public MyException2(String s) {
//super(S);
super(s);
}
}
public class E02_ExceptionClass {
public static void main(String args[]) {
try {
throw new MyException(
"MyException message");
} catch(MyException e) {
e.printMsg();
}
try {
throw new MyException2(
"MyException2 message");
} catch(MyException2 e) {
System.out.println(
"e.getMessage() = " + e.getMessage());
}
}
} ///:~
为什么要用super(s) 在MyException2的基类中有 这个s吗?如果有怎么传过去的?
注销此行 e.getMessage()得到的是 null。