zoukankan      html  css  js  c++  java
  • 课程作业10:异常处理课件中动手动脑的相关问题。

    1.阅读以下代码CatchWho.javaCatchWho2.java,写出程序运行的结果;阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

    CatchWho.java示例代码:

    package ExceptSourceCode07;
    
    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"); 
            } 
        } 
    }

    输出结果截图:


     

    CatchWho2.java示例代码:

    package ExceptSourceCode07;
    
    public class CatchWho2 { 
        public static void main(String[] args) { 
            try {
                    try { 
                        throw new ArrayIndexOutOfBoundsException(); 
                    } 
                    catch(ArithmeticException e) { 
                        System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
                    }
                    /*finally
                    {
                        System.out.println("没捕捉到的内层finally也执行啦!!!");
                    }
                    */
                throw new ArithmeticException(); 
            } 
            catch(ArithmeticException e) { 
                System.out.println("发生ArithmeticException");
            } 
            catch(ArrayIndexOutOfBoundsException e) { 
                System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
            } 
        } 
    }

    输出结果截图:


     

    EmbedFinally.java示例代码:

     

    package ExceptSourceCode07;
    
    
    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");
            
            }
        
        }
    
    }

    输出结果截图:


    总结:

    1.当两层try-catch-finally嵌套时,异常在内层抛出在内层被捕获后,外嵌套其的外层try如果还有其他语句还会继续执行;但是如果内层抛出的异常没有被捕获,则嵌套其的外层try不会继续执行,因为内层try抛出异常,内层又没有捕获异常的语句,则从外层try向外层继续抛出异常,而不会继续执行外层try语句。

    2当多层try-catch-finally嵌套时,异常在不同的层次抛出,在不同的位置抛出,异常就会在不同层次被捕获,同时可能会导致不同的finally语句块执行顺序。

     

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

     

    SystemExitAndFinally.java示例代码:

     

    package ExceptSourceCode07;
    
    
    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");
            
            }
        
        }
    
    
    }

    输出结果截图:


    总结:

           不一定,当try-catch中含有System.exit(0)语句时,程序退出jvm了,不再执行finally语句。

    3.依据对本讲多个示例程序的分析,请自行归纳总结出Java多层嵌套异常处理的基本流程。

    Java多层嵌套异常处理的基本流程总结:

    1.try语句嵌套从外层到内层执行,在try语句中,哪一层出错,哪一层就抛出异常,该层后边的try语句就不再执行,如果该层存在catch就进行相应的捕获,且内层try中嵌套的try-catch语句外部如果还有语句会继续执行;如果该层没有catch进行捕获,就向外抛出,如果外部try-catch语句中也没有catch进行捕获,就终止程序。

    2.try-catch-finally语句嵌套时,内层try抛出异常,即使catch没有捕捉到抛出的异常,内层的finally也一样会执行,然后异常继续向外抛出,除非遇到极特殊的System.exit(0)在finally语句之前的try-catch中,finally语句才不执行。

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

    设计程序代码如下:

    //信1605-2  20160955  张晨阳
    package homework5;
    
    import java.util.Scanner;
    
    public class ExamGradeRank 
    {
        public static void main(String[] args)
        {
            try
            {
                System.out.println("请输入张三的java成绩(取整数部分):");
                Scanner input=new Scanner(System.in);
                int grade;
                int flag=1;
                while(flag!=0)
                {
                    grade=Integer.parseInt(input.next());
                    if(grade>=0&&grade<=100) 
                    {
                        if(grade<60)
                        {
                            System.out.println("不及格");
                        }
                        else if(grade<70)
                        {
                            System.out.println("及格");
                        }
                        else if(grade<80)
                        {
                            System.out.println("中");
                        }
                        else
                        {
                            System.out.println("优");
                        }
                        flag=0;
                    }
                    else
                    {
                        System.out.println("输入成绩的范围不在0~100之间,请重新输入:");
                    }
                }
            }
            catch(NumberFormatException e)
            {
                System.out.println("输入的数据类型有误,输入必须是整数!");
            }
            catch(Exception e)
            {
                System.out.println("程序出现了其他异常!");
            }
        }
    }

    运行结果截图:

  • 相关阅读:
    spring之通过实例工厂方法配置Bean
    spring之通过静态工厂方法配置Bean
    spring之添加后置处理器的bean的生命周期
    spring之未加后置处理器的bean的生命周期
    TreeMap源码分析——基础分析(基于JDK1.6)
    HashMap源码分析(基于JDK1.6)
    left join 和 left outer join 的区别
    开发者必备的6款源码搜索引擎
    理想的技术面试过程
    一篇学习HTTP状态码的神文:我与依依的橙色岁月
  • 原文地址:https://www.cnblogs.com/iCheny/p/7868407.html
Copyright © 2011-2022 走看看