class SelfException extends Exception {
private int detail;
SelfException(int x){
detail=x;
}
/* public String toString() {
return "SelfException "+detail;
}*/
}
class ExceptionDemo{
static void computer(int a)throws SelfException{
System.out.println("called cpmputer("+a+")");
if(a>50)
throw new SelfException(a);
System.out.println("正常退出");
}
public static void main(String args[]) {
try {
computer(10);
computer(100);
}catch(SelfException e) {
System.out.println("catch "+e);
}
}
}
因为它本身就有toString方法,你注释掉的只是重写的方法
所有类其实都可以视为 java.lang.Object 的子类,会自动继承 Object 的toString() 方法。
在自定义类中定义的toString() 方法只是一种重写,会覆盖掉Object 类的toString() 方法,但当你注释掉或没有定义toString() 时就会调用Object 类的toString() 方法