首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

为啥输出c

2013-12-15 
为什么输出cclass MyExceptions extends NumberFormatException{public MyExceptions(){super(c)}publi

为什么输出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了

热点排行