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

关于JAVA异常处理的问题
? 创建一个异常类MyException。
? 创建一个运程程序类,该类包含多个方法Method A、Method B、Method C。这些方法形成三级调用,主方法main调用Method A,Method A调用Method B,Method B调用Method C,最后Method C会抛出一个MyException的实例对象。
? Method A会捕获到Method C抛出的MyException对象两次。
? main方法也会捕获到Method C抛出的MyException对象两次。
? 其运行结果如下所示:
  Method A called!
  Method B called!
  Method C called!
  Exception throw in MyException!
  Exception caught in Method A
  Exception finally statement in Method A
  Exception caught in Main
  Exception finally statement in Main
以下是我的代码:
Java code

public class MyException {

    public static void main (String[] args) {
  
        new MyException().MethodA();
        
    }
    
    public void MethodA(){
        System.out.println("Method A called!");
        try{
            this.MethodB();
        }catch(NewException e){
            System.err.println(e);
            System.out.println("Exception caught in Method A");
        }finally{
            System.out.println("Exception finally statement in Method A");
        }

    }
    
    public void MethodB()throws NewException{
        System.out.println("Method B called!");
        this.MethodC();
    }
    
    public void MethodC()throws NewException{
        System.out.println("Method C called!");
        throw new NewException();
        
    }
    
}

class NewException extends Exception{
    
    public String toString(){
        return ("Exception throw in MyException!");
    }
}


无法做出题目需要的显示结果,因为Method A中已经捕获异常了Main中不是就捕获不到异常了吗?求指教,谁会教教我吧
 

------解决方案--------------------
methodA catch到异常后再抛给main不就行了 你看看methodB不就是抛出去了
------解决方案--------------------
Java code

public class MyException {

    public static void main (String[] args) {
  
        new MyException().MethodA();
        
    }
    
    public void MethodA()[color=#FF0000]throws NewException[/color]{
        System.out.println("Method A called!");
        try{
            this.MethodB();
        }catch(NewException e){
            System.err.println(e);
            System.out.println("Exception caught in Method A");
            [color=#FF0000]throw e[/color]        }finally{
            System.out.println("Exception finally statement in Method A");
        }

    }
    
    public void MethodB()throws NewException{
        System.out.println("Method B called!");
        this.MethodC();
    }
    
    public void MethodC()throws NewException{
        System.out.println("Method C called!");
        throw new NewException();
        
    }
    
}

class NewException extends Exception{
    
    public String toString(){
        return ("Exception throw in MyException!");
    }
}

------解决方案--------------------
探讨

Java code

public class MyException {

public static void main (String[] args) {
  
new MyException().MethodA();

}

public void MethodA()[color=#FF0000]throws NewExc……

------解决方案--------------------
a方法捕获后按照楼上的方法:
Java code

 public void MethodA()[color=#FF0000]throws NewException[/color]{
        System.out.println("Method A called!");
        try{
            this.MethodB();
        }catch(NewException e){
            System.err.println(e);
            System.out.println("Exception caught in Method A");
            [color=#FF0000]throw e[/color]        }finally{
            System.out.println("Exception finally statement in Method A");
        }

这样抛出,在main方法也捕获不到吧 ????????