第七节 动手动脑

一、动手动脑:多层的异常捕获-1

阅读以下代码(CatchWho.java),写出程序运行结果:

public class CatchWho {

    public static void main(String[] args) {

        try {

            try {

                throw new ArrayIndexOutOfBoundsException();

            }

            catch(ArrayIndexOutOfBoundsException e) {

               System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");

            }

 

            throw new ArithmeticException();

        }

        catch(ArithmeticException e) {

            System.out.println("发生ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");

        }

    }

}

程序运行结果:

ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

程序运行结果截图

二、写出CatchWho2.java程序运行的结果

public class CatchWho2 { 
    public static void main(String[] args) { 
        try {
            try { 
                throw new ArrayIndexOutOfBoundsException(); 
            } 
            catch(ArithmeticException e) { 
                System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
            }
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) { 
            System.out.println("发生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
        } 
    } 
}

程序运行结果:

ArrayIndexOutOfBoundsException/外层try-catch

运行结果截图:

三、请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结

public class EmbededFinally {

    
    public static void main(String args[]) {
        
        int result;
        
        try {
            
            System.out.println("in Level 1");

           
             try {
                
                System.out.println("in Level 2");
  // result=100/0;  //Level 2
               
                 try {
                   
                     System.out.println("in Level 3");
                      
                     result=100/0;  //Level 3
                
                } 
                
                catch (Exception e) {
                    
                    System.out.println("Level 3:" + e.getClass().toString());
                
                }
                
                
                finally {
                    
                    System.out.println("In Level 3 finally");
                
                }
                
               
                // result=100/0;  //Level 2

            
                }
            
            catch (Exception e) {
               
                 System.out.println("Level 2:" + e.getClass().toString());
           
             }
             finally {
                
                System.out.println("In Level 2 finally");
           
             }
             
            // result = 100 / 0;  //level 1
        
        } 
        
        catch (Exception e) {
            
            System.out.println("Level 1:" + e.getClass().toString());
        
        }
        
        finally {
           
.             System.out.println("In Level 1 finally");
        
        }
    
    }

}

程序运行结果:

in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

运行结果截图:

分析:

当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机,当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序,当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

四、请通过 SystemExitAndFinally.java示例程序回答上述问题

源代码:

public class SystemExitAndFinally {

    public static void main(String[] args)
    {
        try{

            System.out.println("in main");
            throw new Exception("Exception is thrown in main");
            //System.exit(0);

        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        finally
        {
            System.out.println("in finally");
        }
    }

}

程序运行结果:

in main
Exception is thrown in main

程序运行截图:

分析:

分析:在JVM正常运行的情况下,finally块一定会执行。但如果JVM都退出了finally块就无法执行了

五、运行代码,回答问题

1. 左边的程序运行结果是什么? 2. 你如何解释会得到这样的输出? 3. 计算机是不会出错的,之所以得 到这样的运行结果也是有原因的, 那么从这些运行结果中,你能总 结出Java的哪些语法特性?

程序源代码:

public class ParentChildTest {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();
        
        parent=child;
        parent.printValue();
        
        parent.myValue++;
        parent.printValue();
        
        ((Child)parent).myValue++;
        parent.printValue();
        
    }
}

class Parent{
    public int myValue=100;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}
class Child extends Parent{
    public int myValue=200;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}

程序运行结果:

Parent.printValue( ).myValue=100

Child.printValue( ).myValue=200

Child.printValue( ).myValue=200

Child.printValue( ).myValue=200

Child.printValue( ).myValue=201

结果截图:

 

结果分析:

第一个父类分配了空间,调用自己的构造方法,输出;第二个子类分配了空间,调用自己的构造方法,输出;第三个子类赋值给父类,当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象,由于对象是子类,父类调用的是子类的方法,输出子类的值;

第四个parent.myValue++是对父类中的变量进行自加运算,而parent.printValue()实际上调用的还是子类的构造方法;第五个((Child)parent).myValue++是将parent对象强制转化成Child,所以指向的是Child类中的变量,进行自加运算之后输出

当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。如果子类与父类有相同的的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段,而不是父类中的字段。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。如果子类被当作父类使用,则通过子类访问的字段是父类的。

posted @ 2015-11-14 20:08  萌萌哒、、土豆  阅读(153)  评论(0编辑  收藏  举报