• 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 会被覆盖。
  • 相关阅读:
    聊聊面试-NoClassDefFoundError 和 ClassNotFoundException 区别
    聊聊面试-int和Integer的区别
    数据库char varchar nchar nvarchar,编码Unicode,UTF8,GBK等,Sql语句中文前为什么加N(一次线上数据存储乱码排查)
    SQL Server数据库阻塞,死锁查询
    数据误操作,教你使用ApexSQLLog工具从 SQLServer日志恢复数据!
    IDEA将Maven项目中指定文件夹下的xml等文件编译进classes
    Tomcat 中文乱码,设置UTF-8
    C#实现前向最大匹、字典树(分词、检索)
    23种设计模式汇总
    Head First设计模式——原型模式和访问者模式
  • 原文地址:https://www.cnblogs.com/ice-image/p/14512269.html
Copyright © 2020-2023  润新知