• 多态与异常处理课后习题


    1.

    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("Parent.printValue(),myValue="+myValue);

             }

    }

    class Child extends Parent{

             public int myValue=200;

             public void printValue() {

                       System.out.println("Child.printValue(),myValue="+myValue);

             }

    }

    实验截图:

     

    总结:在父类与子类有相同方法时,如果是子类对象就调用子类方法,父类对象调用父类方法。

    2.CathWho

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

            }

        }

    }

    截图·:

     

    3. CathWho2

    public class CatchWho2 {

        public static void main(String[] args) {

            try {

                try {

      

                throw new ArithmeticException();

            }

            catch(ArithmeticException e) {

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

            }

                    throw new ArrayIndexOutOfBoundsException();

                }

                catch(ArithmeticException e) {

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

                }

          

          

            catch(ArrayIndexOutOfBoundsException e) {

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

            }

        }

    }

    截图

     

    4 .EmbededFinally

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

           

                       }

       

             }

    }

    实验截图:

     

    总结:丢一个包,然后利用catch去捕捉那个包。不同类名的包必须用与包同类型的catch去捕捉。

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

            }

        }

    }

    实验截图:

     

    总结:finally语句一定会执行。

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

       }

    }

    截图:

     

  • 相关阅读:
    H5 _拖放使用
    CSS _text-align:justify;实现两端对齐
    Tips_钉钉免登前端实现
    快速组建的开发团队要怎么活下来?
    程序员,你的安全感呢?
    从自我驱动到带领10人团队
    你会给别人提反馈吗?
    简单几步成为微信公众平台开发者
    你了解javascript中的function吗?(1)
    容器之路 HashMap、HashSet解析(一)
  • 原文地址:https://www.cnblogs.com/zhng921/p/4955709.html
Copyright © 2020-2023  润新知