日期:2014-05-20  浏览次数:20790 次

为什么输出c
class MyExceptions extends NumberFormatException{
public MyExceptions(){
super("c");
}
public MyExceptions(String s){
super("b");
}
}
public class Test5 {
public static void show1(String s){
try{
System.out.println(Integer.parseInt(s));
}catch(NumberFormatException e){

throw new MyExceptions();
}
}
public static void show2(String s){
try{
System.out.println(Integer.parseInt(s));
}catch(NumberFormatException e){
//throw new MyExceptions();
throw new MyExceptions("a");
}
}

public static void main(String[] args) {
try{
show1(null);
}catch(NumberFormatException e){
System.out.println(e.getMessage());
}
//show2(null);
}

}

------解决方案--------------------
楼主认为应该输出什么呢?
throw new MyExceptions();
这句话会调用MyExceptions类的不带参数的构造函数创建一个对象:
public MyExceptions(){
    super("c");
}

然后将这个对象传递给e
catch(NumberFormatException e){
    System.out.println(e.getMessage());
}
打印的当然就是c了