zoukankan      html  css  js  c++  java
  • 多态与异常处理课后作业

    动手动脑:多层的异常捕获-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语句块就不会执行

  • 相关阅读:
    MATLAB新手教程
    关于Core Location-ios定位
    《TCP/IP具体解释卷2:实现》笔记--IP的分片和重装
    利用JasperReport+iReport进行Web报表开发
    Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系
    Openfire开发配置,Openfire源码配置,OpenFire二次开发配置
    在Activity中为什么要用managedQuery()
    24点经典算法
    linux概念之时间与时区
    java实现第五届蓝桥杯大衍数列
  • 原文地址:https://www.cnblogs.com/sunmei20142925/p/4966410.html
Copyright © 2011-2022 走看看