zoukankan      html  css  js  c++  java
  • java异常的嵌套和级联

    一、分开捕获或者嵌套使用

    我们先看看下面这段代码:

    public class Cal {
    
        public int div(int a, int b) {
            int result = a / b;
            return result;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int s = 0;
    
            int num1 = 0;
            int num2 = 0;
    
            //1、这里可能会抛出异常
            System.out.print("num1=");
            num1 = scanner.nextInt();
            System.out.print("num2=");
            num2 = scanner.nextInt();
    
            Cal cal = new Cal();
            //2、这里也可能抛出异常
            s = cal.div(num1, num2);
    
            System.out.println(s);
        }
    }

    在这段代码中有可能抛出异常的有两个地方,那么我们应该如何处理呢。

    1、当然我们可以分开捕获。如下面的代码:

    public class Cal {
    
        public int div(int a, int b) {
            int result = a / b;
            return result;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int s = 0;
    
            int num1 = 0;
            int num2 = 0;
                  //1、这里可能会抛出异常
            try {
                
                System.out.print("num1=");
                num1 = scanner.nextInt();
                System.out.print("num2=");
                num2 = scanner.nextInt();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            Cal cal = new Cal();
                    //2、这里也可能抛出异常
            try {
                s = cal.div(num1, num2);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            System.out.println(s);
        }
    }

    2、我们还可以在try里面嵌套的使用try语句。如下面代码所示:

    public class Cal {
    
        public int div(int a, int b) {
            int result = a / b;
            return result;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int s = 0;
    
            int num1 = 0;
            int num2 = 0;
    
            try {
                //1、这里可能会抛出异常
                System.out.print("num1=");
                num1 = scanner.nextInt();
                System.out.print("num2=");
                num2 = scanner.nextInt();
                
                try {
                    Cal cal = new Cal();
                    //2、这里也可能抛出异常
                    s = cal.div(num1, num2);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            
    
            System.out.println(s);
        }
    }

    二、使用级联捕获异常

    上面介绍的这两种方法都不是好办法,以为过多的使用try捕获异常会影响程序的效率。所以我们推荐使用的是级联捕获异常。格式如下

    try{
    
    …...
    
    }catch(ArrayIndexOutOfBoundsException e) {
    
    ……
    
    } catch(ArithmeticException e) {
    
    ……
    
    } catch(Exception e) {
    
    ……
    
    }

    注意:使用多重 catch 语句时,异常子类一定要位于异常父类之前。

    所以以下这种方式是错误的。

    try{
    
    …...
    
    } catch(Exception e) {
    
    ……
    
    } catch(ArrayIndexOutOfBoundsException e) {
    
    ……
    
    }

    好,那么我们可以修改上面的代码如下:

    public class Cal {
        public int div(int a, int b) {
            int result = a / b;
            return result;
        }
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int s = 0;
    
            int num1 = 0;
            int num2 = 0;
            try {
                //1、这里可能会抛出异常
                System.out.print("num1=");
                num1 = scanner.nextInt();
                System.out.print("num2=");
                num2 = scanner.nextInt();
    
                Cal cal = new Cal();
                //2、这里也可能抛出异常
                s = cal.div(num1, num2);
            } catch (ArithmeticException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (InputMismatchException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(s);
        }
    }

    由于多次的使用try或影响效率。所以我们如果碰到循环的时候,应该把try语句放到循环的外面,例如我们并不推荐你这样写代码:

    public static void main(String[] args) {
            int[] arr = { 1, 2, 3, 4 };
            Cal cal = new Cal();
    
            for (int i = 0; i < arr.length; i++) {
                try {
                    int s = cal.div(arr[i], 2);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }

    你可以修改成为这样:

        public static void main(String[] args) {
            int[] arr = { 1, 2, 3, 4 };
            Cal cal = new Cal();
            try {
                for (int i = 0; i < arr.length; i++) {
                    int s = cal.div(arr[i], 2);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
  • 相关阅读:
    SQL Server 存储过程
    Prettier
    VSCode常用插件之open in browser使用
    VSCode常用插件之vscode-fileheader使用
    (四)Vue面试题解析,目标高级前端开发者必经之路
    (三)Vue必考面试题解析,继续向高级前端迈进
    (二)Vue常见面试题,向高级前端开发迈进
    (一)Vue常见面试题,看看你都会了吗?
    Vue2.x 开发必须知道的 36 个技巧
    开箱即用 vue-cli4 vant rem 移动端框架方案
  • 原文地址:https://www.cnblogs.com/weibanggang/p/11184697.html
Copyright © 2011-2022 走看看