美文网首页
try catch finally 中包含return的几种情况

try catch finally 中包含return的几种情况

作者: zhengaoly | 来源:发表于2021-10-15 13:55 被阅读0次

    try catch finally 中包含 return 的几种情况,及返回结果

    当当当,兴致勃勃的第二篇博客,散花~

    下面是正题(敲黑板)

    第一种情况:在 try 和 catch 中有 return,finally 中没有 return,且 finally 中没有对 try 或 catch 中要 return 数据进行操作的代码,这种情况也是最好理解的。

    public class Test { public static int num=1; public static void main(String[] args) throws ParseException { int result;
            result = num();
            System.out.println(result);
        } private static int num() { try{ int b=4/0; return num = num+2;
            }catch(Exception e){ return num = num+3;
            }finally {
            System.out.println("不管你怎么样,我都是要执行");    
            }        
        }    
    }
    

    输出内容为:
    不管你怎么样,我都是要执行
    4

    原因,int b=4/0; 发生了异常,直接进入 catch 的代码块中执行了 return num = num+3; 此时把返回的结果 4。如果没有异常的话会执行 try 中的 return,catch 中的代码不会执行,但是无论怎样,finally 中的代码都会执行。

    第二种情况:在 try 和 catch 中有 return,finally 中没有 return,但 finally 中有对 try 或 catch 中要 return 数据进行操作的代码

    要返回的数据是基本数据类型还是引用数据类型,对结果也有不同的影响

    返回的数据为基本数据类型,则 finally 中对要返回数据操作无影响

    public class Test { public static int num=1; public static void main(String[] args){ int result;
            result = num();
            System.out.println(result);//结果不受finally影响,输出4
            System.out.println(num);//5
     } private static int num() { try{ int b=4/0; return num = num+2;
            }catch(Exception e){ return num = num+3;
            }finally { ++num;
            }        
        }    
    }
    

    result 的值为 4 的原因是,当执行到 catch 中的 return num = num+3; 时,已经把要返回的 num 的值存到了其他局部变量中,在执行完 finally 中的 ++num; 后,是从其他局部变量中获取的返回值,而不是直接返回 num 的值

    image

    返回的数据为引用数据类型,finally 中如果改变了返回对象的属性则影响结果,如果改变的是对象的引用则和基本数据类型一样不改变结果

    public class Test { public static void main(String[] args) {
            People bride;
            bride = marry();
            System.out.println(bride.getState());//结果受finally影响,输出dead
        } private static People marry() {
            People people=new People();
            people.setState("happy");; try{ int b=4/0; return people;
            }catch(Exception e){ return people;
            }finally {
                people.setState("dead");
            }        
        }    
    }
    

    bride.getState()的结果为dead的原因是,当执行到catch中的return people;时,把要返回people的内存地址存储起来,但是finally中对该内存地址对象的属性进行了更改,bride = marry();
    获取的内存地址对应的对象是更改属性后的people,所以属性值改变了。</pre>

    image

    第三种情况:在 try 和 catch 中有 return,finally 中也有 return

    try 或 catch 中 return 后面的代码会执行,但最终返回的结果为 finally 中 return 的值,需要注意的是 try 或 catch 中 return 后面的代码会执行****,只是存起来了,并没有返回,让 finally 捷足先登先返回了********

    public class Test { public static int num=1; public static void main(String[] args) throws ParseException { int result;
            result = num();
            System.out.println(result);//输出结果为1003
            System.out.println(num);//输出结果为1001
     } private static int num() { try{ int b=4/0; return num = num+1000;
            }catch(Exception e){ return num = num+1000;
            }finally { return num+2;
            }        
        }    
    }
    
    image

    第四种情况:在 try 中有 return,在 catch 中新抛出异常,finally 中有 return

    如果 catch 块中捕获了异常, 并且在 catch 块中将该异常 throw 给上级调用者进行处理, 但 finally 中有 return, 那么 catch 块中的 throw 就失效了, 上级方法调用者是捕获不到异常

    public class Test { public static void main(String[] args) throws Exception {
            String result=""; try {
                result = num();
            } catch (Exception e) {
                System.out.println("青天大老爷在此");
            }
            System.out.println(result);
        } public static String num() throws Exception { try{ int b=4/0; return "总是异常,反正我又不会执行";
            }catch(Exception e){ throw new Exception();
            }finally { return "用金钱蒙蔽你的双眼";
            }    
        }    
    }
    
    用金钱蒙蔽你的双眼
    如果把finally里的return注释掉就会输出:
    青天大老爷在此
    

    结束语:try catch finally 的情感纠纷到此结束,大家看完是不是觉得 finally 就是一个小婊咂呢?其实 finally 的用处是很大的。

    它是为异常处理事件提供的一个清理机制,一般是用来关闭文件或释放其他系统资源。

    finally 只有一种情况不会执行。当执行到 System.exit(0);finally 就不会执行。

    相关文章

      网友评论

          本文标题:try catch finally 中包含return的几种情况

          本文链接:https://www.haomeiwen.com/subject/jdrmoltx.html