动手动脑及课堂作业

一、运行课件中的例题ParentChildTest.java,回答下列问题:

a、左边的结果运行结果是什么?

运行结果截图为:

b、你如何解释会得到这样的输出?

第一个值:父类Parent调用自己的方法;

第二个值:子类child调用方自己的方法;

第三个值:子类将自己的方法赋给父类,因而实际调用的是子类的方法;

第四个值:父类中的值MyValue加一,但是父类调用的是子类的方法,因而无影响;

第五个值:强制转化,父类将值赋给子类,调用的仍然是子类的方法,只是值加一

c、计算机是不会出错的,之所以会得到这样的运行结果也是有原因的,那么从运行结果当中,你能总结出java的那些语法特征?

① 用类名进行赋值时(如parent=child ),附的都是方法;

② 对类中的值进行赋值时需要用类名.对象;

③ 父类对子类进行赋值时需要强制转化;

(1)、并修改ParentChildTest.java,验证你的回答结果。

  验证程序代码如下:

 

public class ParentChileTest {

public static void main(String[] args) {

Parent parent=new Parent();

parent.printValue();

Child child=new Child();

child.printValue();

parent=child;

parent.printValue();

parent.myValue ++;

System.out.println(parent.myValue);

System.out.println(child.myValue);

parent.printValue();

((Child)parent).myValue++;

System.out.println(parent.myValue);

System.out.println(child.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);

}

}

截图:

 

二、请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识

实验代码为:

import javax.swing.*;

class AboutException {

   public static void main(String[] a) 

   {

      int i=1, j=0, k;

      k=i/j;

try

{

k = i/j;    // Causes division-by-zero exception

//throw new Exception("Hello.Exception!");

}

catch ( ArithmeticException e)

{

System.out.println("被0除.  "+ e.getMessage());

}

catch (Exception e)

{

if (e instanceof ArithmeticException)

System.out.println("被0除");

else

{  

System.out.println(e.getMessage());

}

}

finally

     {

      JOptionPane.showConfirmDialog(null,"OK");

     }

  }

}

实验截图为:

 

使用Java异常处理的机制:

把可能会发生错误的代码放进try语句块中。

当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。

当异常发生时,程序控制流程由try语句块跳转到catch语句块。

不管是否有异常发生,finally语句块中的语句始终保证被执行。

如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

三、多层的异常捕获-1

   阅读下面代码,写出程序的运行结果:

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

截图为:

 

四、写出下列程序的运行结果:

  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

结果截图:

五、当有多个嵌套的trycatchfinally时,要特别注意finally的执行时机。

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

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

实例:

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之前的catch都运行,之后只运行finally

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

        }

    }

}

运行结果截图:

 

Finally不一定都要执行,系统遇到System.Exit0)是便会中止;

trycatchfinally的关系:try快不能单独存在,后面必须跟catch块或者finally;三者之间的组合为:try-finally   .try-catch  .try-catch-finally这几种是合法的;一个try快可以有多个catch,从上到下catch的范围是从小到大。

七、编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。(要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。)

源代码为:

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Grade {

public static void main(String[] args) {

// TODO 自动生成的方法存根

double i;

for(;;)

{

Scanner in = new Scanner(System.in);

try{

System.out.println("请输入成绩:(0-100)");

i = in.nextInt(); 

if(i<0||i>100)

{System.out.println("您输入的信息有误!");break;}

if(i>=0&&i<60)

{System.out.println("不及格!");break;}

if(i>=60&&i<70)

{System.out.println("及格");break;}

if(i>=70&&i<80)

{System.out.println("中");break;}

if(i>=80&&i<90)

{System.out.println("良");break;}

if(i>=90&&i<=100)

{System.out.println("优秀!");break;}

}

  catch(Exception e)

{

System.out.println("您输入的信息有误!");

}

}

}

}

运行结果截图:

 

原文地址:https://www.cnblogs.com/huangliping/p/4964599.html