• 7多态与异常处理的课上作业


    1.怎样判断对象是否可以转换?可以使用instanceof运算符判断一个对象是否可以转换为指定的类型:

    Object obj="Hello";

    if(obj instanceof String)

    System.out.println("obj对象可以被转换为字符串");

    1下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

      m=d;         //编译正确,因为子类对象可以直接赋给基类变量。

              d=m;        //编译错误,基类对象要赋给子类对象变量,必须执行类型转换(子类对象变量=(子类名)基类对象名;

              d=(Dog)m;   //编译正确,基类对象要赋给子类对象变量,必须执行类型转换

              d=c;         //编译错误,不同子类之间不能复制

              c=(Cat)m;    //m已经转换成Dog DogCat子类之间不能转换

    2)先进行自我判断,得出结论后,运行TestCast.java实例代码,看看你的判断是否正确.

    源代码:

    class Mammal{}

    class Dog extends Mammal {}

    class Cat extends Mammal{}

    public class TestCast

    {

    public static void main(String args[])

    {

    Mammal m;

    Dog d=new Dog();

    Cat c=new Cat();

    m=d;

    //d=m;

    d=(Dog)m;

    //d=c;

    //c=(Cat)m;

    }

    }

    运行结果截图:

     

    (3)运行以下测试代码,回答问题:

    源代码:

    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();

         }

    }

    1. 】  左边的程序运行结果是什么?

    2.】   你如何解释会得到这样的输出?

    3.】   计算机是不会出错的,之所以得到这样的运行结果也是有原因的,那么从这些运行结果中,你能总结出Java的哪些语法特性?

    注:实验报告中任务一已做了解释。

    2.动手动脑:多层的异常捕获

    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"

    截图:

     

    (2)写出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"

    截图:

     

    3.请先阅读 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");

    }

    }

    }

    总结:当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

    运行结果截图:

     

    4.辨析:finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题。

    finally不一定执行,当try从句或者catch()从句中有System.exit(0)语句时,finally()不会被执行。

    源代码:

    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");

            }

        }

    }

    运行结果截图:

     

    5.如何跟踪异常的传播路径?请通过 PrintExpressionStack.java示例掌握上述内容。

    源代码:

    // UsingExceptions.java

    // Demonstrating the getMessage and printStackTrace

    // methods inherited into all exception classes.

    public class PrintExceptionStack {

       public static void main( String args[] )

       {

          try {

             method1();

          }

          catch ( Exception e ) {

             System.err.println( e.getMessage() + " " );

             e.printStackTrace();

          }

       }

       public static void method1() throws Exception

       {

          method2();

       }

       public static void method2() throws Exception

       {

          method3();

       }

       public static void method3() throws Exception

       {

          throw new Exception( "Exception thrown in method3" );

       }

    }

    运行结果截图;

     

    总结:当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

    可使用printStackTrace 和 getMessage方法了解异常发生的情况:printStackTrace:打印方法调用堆栈。每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

    6.归纳与总结.

    依据对本讲多个示例程序的分析,请自行归纳总结出Java多层嵌套异常处理的基本流程。

    阅读任何一本Java教材,或者是使用互联网搜索引擎,查找有关Java多层嵌套异常处理流程的资料,看看你总结得是否全面。

    Java多层嵌套异常处理的基本流程:

    首先Throwable类有两个直接子类:

    1Exception:出现的问题是可以被捕获的;

    2Error:系统错误,通常由JVM处理。

    可捕获的异常又可以分为两类:

    1check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出。

    2runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象。

    然后

    try-catch-finally相互嵌套时,先处理最内层的try-catch-finally。当try抛出了与catch匹配的异常,则代码到相应的catch()中执行。如果catch也出现了异常,程序会检测finally中是否有异常,若有,则覆盖。如果只有try-finally,那么先执行finally,如果finally没有异常,则返回处理try中的异常,如果finally有异常,则覆盖try中的异常。

  • 相关阅读:
    Android:RelativeLayout相对布局(转)
    Android:LayoutInflater作用及使用(转)
    keepalive安装配置
    nginx 多级7层代理安装配置
    k8s使用cephfs
    haproxy安装及配置
    通过nodeSelector配置项实现pod部署至指定node
    SFTP使用key文件登录
    k8s使用ceph存储
    nginx开启gzip压缩后导致apk包下载不能正常安装
  • 原文地址:https://www.cnblogs.com/fan-xiaofan/p/4967543.html
Copyright © 2020-2023  润新知