码迷,mamicode.com
首页 > 其他好文 > 详细

多态与异常处理课后习题

时间:2015-11-11 13:23:36      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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() + "\n" );

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

   }

}

截图:

 技术分享

多态与异常处理课后习题

标签:

原文地址:http://www.cnblogs.com/zhng921/p/4955709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!