• JAVA 多态和异常处理作业——动手动脑以及课后实验性问题


    1、  阅读以下代码(CatchWho.java),写出程序运行结果:

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

            }

        }

    }

    2)  运行结果

    1、  写出CatchWho2.java程序运行的结果

    1、1)源代码

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

            }

        }

    }

    1)  运行结果

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

    1)源代码

     

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

            }

        }

    }

    2)运行结果

    1)  总结

    先执行try语句,有catch执行,最后执行finally语句。

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

        1)源代码

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

            }

        }

     

    }

    2)运行结果

    3)总结

    Finally语句不一定执行,在catch语句中,有exit(0)语句即退出。要看具体情况,catch语句的处理。

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

    1)源代码

    import java.io.*;

    public class Grade {

     

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            String a = null;

            int g;

            BufferedReader strin =new BufferedReader(new InputStreamReader(System.in));

            try

            {

                System.out.println("请输入成绩(整数): ");

                a =  strin.readLine();

            }

            catch(Exception e)

            {

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

            }

            try

            {

                g = Integer.parseInt(a);

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

                    System.out.println("优秀");

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

                    System.out.println("良好");

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

                    System.out.println("中");

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

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

                else if(g<60)

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

                else

                    System.out.println("分值错误");

            }

            catch(NumberFormatException e)

            {

                System.out.println("输入错误");

            }

       

        }

     

    }

    2)运行结果

  • 相关阅读:
    Upgrading CentOS 6 to CentOS 7
    redis主从同步错误处理
    【linux】free命令中cached和buffers的区别
    服务器TIME_WAIT和CLOSE_WAIT区别及解决方案
    msyql 主从切换
    MySQL主从同步报错1507
    MYSQL SHOW 用法
    NGINX中的proxy_pass和rewrite
    Web服务器Nginx多方位优化策略
    operator重载运算符
  • 原文地址:https://www.cnblogs.com/justmaomao/p/4963399.html
Copyright © 2020-2023  润新知