zoukankan      html  css  js  c++  java
  • JAVA09异常处理之动手动脑问题

    动手动脑1:为什么不管是否有异常发生,finally语句块中的语句始终保证被执行?

    我们在写代码时,如果finally块中的代码过多会导致字节码条数”膨胀”,因为finally中的字节码会被”复制”到try块和所有的catch块中。finally语句块主要用于解决资源泄露问题,它位于catch语句块之后,JVM保证它们一定执行。

    动手动脑2:CatchWho.java,写出程序运行结果:

    ArrayIndexOutOfBoundsException/内层try-catch
    发生ArithmeticException

    运行结果截图:

    结果相同。

    动手动脑3:CatchWho2.java,写出程序运行结果:

    ArrayIndexOutOfBoundsException/外层try-catch

    运行结果截图:

    结果相同。

    动手动脑4:当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。
    请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
    特别注意:
    当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

    EmbedRinally.java程序如下:

    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) {//exception异常
                        
                        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");
            
            }
        
        }
    
    }

    程序运行结果:

    总结:①catch语句一定和相邻的try,才可以接收到抛出的错误。

    ②finally语句是不管有没有异常都可以执行。

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

    SystemExitAndFinally.java

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

    程序运行结果:

    “System.exit(0);”:System是一个Java类,调用exit(0)方法终止虚拟机也就是退出你的Java程序,括号里面的是参数,进程结束的返回值。即是强制退出程序。

    “System.exit(1);”:即是异常终止。

    因为catch中有“System.exit(0);”,使得程序终止,所以不会执行finally语句,但一般情况下,finally都会执行。

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

    程序代码:

    package Test;
    
    import java.util.Scanner;
    
    //编写一个程序,此程序在运行时要求用户输入一个    整数
    //代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
    //李慧,2016.11.22
    
    //三种异常错误类
    class MyException extends Exception{
        public MyException(String e){
            super(e);
        }
    }
    class NumException extends Exception{
        public NumException(String e){
            super(e);
        }
    }
    class CCharException extends Exception{
        public CCharException(String e){
            super(e);
        }
    }
    
    public class Score {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String str;
            int score;
            Scanner scan=new Scanner(System.in); 
         System.out.print("请输入语文成绩(整数):");
         str=scan.next();
        int x=1;
        if(!str.matches("^[0-9]+$")){//是字符串-不是数字
             try{throw new CCharException("出错!应输入数字!"); }
             catch(CCharException e){
                 System.out.println(e);}
             finally {System.out.println();} }
      else//不是字符串-是数字
        { 
          double num= Double.parseDouble(str);
            int a=(int)num;
            //System.out.println(num);
            //System.out.println(a);
         if(num!=a){//输入的内容是double型
            try{throw new NumException("出错!应输入整数!"); }
        catch(NumException e){
                System.out.println(e);}
            finally {System.out.println();}
        }
        else if(a<0 || a>100)
        {  try{
            throw new MyException("出错!成绩范围应为0~100分!");
            }catch(MyException e){
                System.out.println(e);
            }finally {
              System.out.println();
             }}
        else{
         if(a<60) System.out.println("不及格!");
         else if(a>=60 && a<70) System.out.println("及格!");
         else if(a>=70 && a<80) System.out.println("中!");
         else if(a>=80 && a<90) System.out.println("良!");
         else if(a>=90 && a<=100) System.out.println("优!");
        }
         }//else
    }//main
    }

    程序运行截图:

  • 相关阅读:
    你都这么拼了,面试官TM怎么还是无动于衷
    js中string转map的方法
    如何使用jmeter做一个功能的性能测试
    如何看待远程办公?
    vue.js指令v-for使用以及下标索引的获取
    v-charts x轴字体斜显示
    Linux-(inotify-tools&rsync)
    Linux-(type,vim)
    zab协议
    数据库的规范一览
  • 原文地址:https://www.cnblogs.com/xiaxiaoshu/p/6092460.html
Copyright © 2011-2022 走看看