这段代码可以输出"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");
}
}
}
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");
}
}
}
System.out.println(e.getMessage()); 就会输出"Out of money"
System.out.println(e.getMessage());
System.out.println("Wrong");
就可以输出了
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]
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");
}
}
}