• Java多层的异常捕获


     

    一、多层异常捕获示例1:

     

    运行结果:

     

    原因分析:

    此题有两个try-catch异常捕获,第一个throw抛出的错误,被内层catch捕获,故最后一个catch未捕获,不显示;第二个catchArithmeticException,被同名即第二个catch捕获,显示发生ArithmeticException。

    二、多层异常捕获示例2:

     

    运行结果:

     

    原因分析:

    通过Debug运行分析知,当第一个throw抛出错误后,直接跳转到最后一个同名的catch捕获块,中间程序未运行。故总结,Java中,使用try-catch语法,一旦出错,就捕获该错误;若注销第一个throw错误,则会运行第二个catch,显示发生ArithmeticException。

    三、多个try-catch-finall嵌套,方法总结:

    示例:

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

           

                }

       

         }

    }

    运行结果:

     

    原因分析:

    本程序共三个try-catch-finally嵌套,每个try、catch、finally均有输出语句。输出顺序为从第一个try开始执行三次,catch仅执行最里层level3,finally从最里层向外执行。

    Finally主要用于解决资源泄露问题,它位于catch语句块后,JVM保证它一定执行,因此从最里层执行,毫无疑问。

    由于finally语块中可能发生异常,比如此处的level3就发生java.lang.ArithmeticException异常,一旦发生此种异常,先前异常就会被抛弃,故仅仅最里层的catch捕获到异常,之后由于异常被抛弃,level2、level3的catch并未捕捉到异常不显示。

    另外根据try-catch方法使用,try语句块一有异常,则找相应catch捕获经验得知,三个try中均为异常错误,故依次执行try中语句块。

    四、try-catch-finally中finally不执行的特殊情况分析:

    示例:

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

           

                }

       

         }

    }

    运行截图:

     

    原因分析:

    通常情况下,finally运行语句一定执行,但本题中有特殊情况,在catch中有“System.exit(0);”执行此语句后,就已经结束程序,故不会运行finally语句。

    五、编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

    要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

    (一)源程序:

    import java.io.IOException;

    import java.util.Scanner;

    public class TestScore {

         public static void main(String[] args)throws IOException {

                // TODO Auto-generated method stub

                boolean flag=true;

                while(flag) {

                       try {

                              System.out.println("输入学生分数:");

                              Scanner in =new Scanner(System.in);

                              int score=in.nextInt();

                              if(score<=100&&score>=0) {

                                     //正常分数

                                     if(score>=90) {

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

                                            break;

                                     }

                                     else if(score>=80) {

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

                                            break;

                                     }

                                     else if(score>=70) {

                                            System.out.println("中!");

                                            break;

                                     }

                                     else if(score>=60) {

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

                                            break;

                                     }

                                     else if(score>=0) {

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

                                            break;

                                     }

                       }

                              else//不正常int型分数

                                     System.out.println("输入格式错误,请重新输入!");

                             

                       }catch(Exception e) {

                              //输入格式错误

                              System.out.println("输入格式错误,请重新输入!");

                              flag=true;

                       }

                }

                      

         }

    }

    (二)程序结果截图:

     

    (三)结果分析:

    (1)首先对输入格式分析是否正确,正确继续运行,不正确catch捕捉错误,通过while循环再次输入。

    (2)若输入格式正确,但是分数不在正常范围内,则通过if-else判定,提示再次输入。

    (3)使用if-else if()判断分数等级。

  • 相关阅读:
    python 第三方库大全
    windows CMD实现的信息收集工具
    ip协议是哪一层的协议
    MetaWeblog访问地址
    通过卸载程序触发恶意进程
    IP地址查询接口
    mysql 密码忘记解决办法
    查询IP地址的免费API
    showdan
    【译】android的审计和hacking工具
  • 原文地址:https://www.cnblogs.com/somedayLi/p/7847136.html
Copyright © 2020-2023  润新知