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

多态与异常处理课后作业

时间:2015-11-15 13:32:54      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

动手动脑:多层的异常捕获-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"); 

        } 

    } 

}

程序结果截图:

 技术分享

动手动脑:多层的异常捕获-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"); 

        } 

    } 

}

程序结果截图:

 技术分享

辨析:finally语句块一定会执行吗?

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

public class Test {

 

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

        }

    }

 

}

程序结果截图:

 技术分享

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

源程序代码:

import java.util.Scanner;

 

class   AException   extends Exception

{

String error;

AException ()  

{

error="请注意要求是输入0~100的数字";   

}

public String geterror()

{

return error;

}

}

 

class Test

    public static void main(String args[]) 

    { 

       Scanner sc = new Scanner(System.in); 

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

 

        try 

        { 

            String s = sc.nextLine(); //读入屏幕的类型转换为S类型    

            getnum(s);//判断是否抛出异常 

        } 

        catch (AException e) 

        { 

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

        } 

 

    } 

 

    private static void getnum(String s) throws AException 

    { 

     for (int i = s.length(); --i >= 0;) 

        {

         int chr = s.charAt(i);

            if (chr < 48 || chr > 57)

            {

             throw new AException(); 

            }

 

        }  

     double num = Double.parseDouble(s);   

        if (num < 0 || num> 100) 

        { 

            throw new AException(); 

        } 

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

        { 

            System.out.print("不及格"); 

        } 

        else if (num >= 60 && num <= 70) 

        { 

            System.out.print("及格"); 

        } 

        else if (num>= 70 && num<= 80) 

        { 

            System.out.print(""); 

        }

        else if (num >= 80 && num <= 90) 

        { 

            System.out.print(""); 

        } 

        else 

        { 

            System.out.print(""); 

        } 

    } 

}

 

问题解答:

只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行。如果在try语句块之前返回(return)或者抛出异常,try对应的finally语句块就不会执行

多态与异常处理课后作业

标签:

原文地址:http://www.cnblogs.com/sunmei20142925/p/4966410.html

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