这段代码可以输出"Out of money"public class WithReturn { public int methodA(int money)throws SpecialException{
if(--money<=0) throw new SpecialException("Out of money");
return money;
}

public int methodB(int money){
try{
System.out.println("Begin");
int result=methodA(money);
return result;
}catch(SpecialException e){
System.out.println(e.getMessage());
return -100;
}finally{
System.out.println("Finally");
}
}
public static void main(String[] args) {
System.out.println(new WithReturn().methodB(1));
}
}
下面这段代码就不能输出"Out of money"请问为什么?
public class MainCatcher { public void methodA(int money)throws SpecialException{
if(--money<=0) throw new SpecialException("out of money");
System.out.println("methodA");
}
public void methodB(int money)throws SpecialException{
methodA(money);
System.out.println("methodB");
}

public static void main(String[] args) {
try{
new MainCatcher().methodB(1);
System.out.println("main");
}catch(SpecialException e){
System.out.println("Wrong");
}
}
}

解决方案 »

  1.   

    你定义了throw new SpecialException("Out of money"); 
    System.out.println(e.getMessage()); 就会输出"Out of money"
    System.out.println(e.getMessage());
    System.out.println("Wrong");
    就可以输出了
      

  2.   

    加上System.out.println(e.getMessage());就行了public class MainCatcher { public void methodA(int money) throws Exception {
    if (--money <= 0)
    throw new Exception("out of money");
    System.out.println("methodA");
    } public void methodB(int money) throws Exception {
    methodA(money);
    System.out.println("methodB");
    } public static void main(String[] args) {
    try {
    new MainCatcher().methodB(1);
    System.out.println("main");
    } catch (Exception e) {
    [code=Java]System.out.println(e.getMessage());
    System.out.println("Wrong");
    }
    }
    }
    [/code]
      

  3.   

    上面的代码乱了!!!public class MainCatcher { public void methodA(int money) throws Exception {
    if (--money <= 0)
    throw new Exception("out of money");
    System.out.println("methodA");
    } public void methodB(int money) throws Exception {
    methodA(money);
    System.out.println("methodB");
    } public static void main(String[] args) {
    try {
    new MainCatcher().methodB(1);
    System.out.println("main");
    } catch (Exception e) {
    System.out.println(e.getMessage());
    System.out.println("Wrong");
    }
    }
    }
      

  4.   

    你没打印 System.out.println(e.getMessage());谁给你输出 OutOfMemory?!