zoukankan      html  css  js  c++  java
  • 003-try-catch-finally-return执行顺序问题

    一、try-catch-finally-return执行顺序问题

    0、原始执行顺序

    try — > finally

    try —> catch —> finally

    1、try catch 中有 return,finally 中无 return,且 try 中无异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
        }
    }
    /**
    执行结果:
    try: i = 2
    finally: i = 5
    2
    */
    

    2、try catch finally中均有 return,try中无异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
            return i;
        }
    }
    /**
    返回结果5
    try: i = 2
    finally: i = 5
    5
    */
    

    3、try catch 中有 return,finally 中无 return,且 try 中有异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
        }
    }
    /**
    返回结果4
    exception: i = 4
    finally: i = 7
    4
    */
    

    4、try catch finally中均有 return,try中有异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
            return i;
        }
    }
    /**
    返回结果5
    exception: i = 4
    finally: i = 7
    7
    */
    

    5、try、catch 中有 return,try finally 中有异常,finally 中的异常有 return

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            try {
                int b = i/0;
            } catch (Exception e) {
                 i = i + 5;
                 return i;
            }
            System.out.println("finally: i = " + i);
        }
    }
    /**
    exception: i = 4
    12
    */
    

    6、try、catch 中有 return,try finally 中有异常,finally 中的异常无 return

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            try {
                int b = i/0;
            } catch (Exception e) {
                 i = i + 5;
            }
            System.out.println("finally: i = " + i);
        }
    }
    /**
    exception: i = 4
    finally: i = 12
    4
    */
    

    总结

    • finally 的代码总会被执行
    • try、catch 中有 return 的时候,也会执行 finally。return 的时候,要注意返回值是否会受到 finally 中代码的影响。
    • finally 中有 return 的时候,会直接在 finally 中退出,导致 try、catch中的 return 失效。
    • finally 中如果有异常的时候,且存在 return 的时候,catch 的 return 会被覆盖。
  • 相关阅读:
    【转】BT5汉化步骤
    创建MySql用户并给用户授权,设置mariadb默认编码,修改mariadb数据存储目录datadir
    《Hadoop实战》陆嘉恒著读书笔记
    org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException: The content of elements must consi
    js获取两个日期之间间隔的天数。兼容IE6、7、8、9,Firefox,Chrome
    mac下下载安装Android Studio教程
    Android Studio下载安装教程及开发环境搭建
    【eoe资源】如何让Android ListView的平滑滚动
    安装Android Studio遇到中文乱码的解决方法
    最新android studio注意事项打不开等问题6种解决方法
  • 原文地址:https://www.cnblogs.com/ice-image/p/14512269.html
Copyright © 2011-2022 走看看