资讯专栏INFORMATION COLUMN

try-catch-finally中的return

fizz / 942人阅读

摘要:基础系列的与方法类初始化顺序线程池如何弹性伸缩的几个要点的缓存什么场景下使用阻塞队列的使用及模式中的序本文主要简述中有的情况。参考关于中的执行顺序

Java基础系列

Java的hashcode与equals方法

Java类初始化顺序

ThreadPoolExecutor线程池如何弹性伸缩

HashMap的几个要点

Integer的缓存

什么场景下使用阻塞队列

volatile的使用及DCL模式

try-catch-finally中的return

本文主要简述try-catch-finally中有return的情况。笔试面试经常考到。

题目
/**
 * http://qing0991.blog.51cto.com/1640542/1387200
 * Created by codecraft on 2016-03-08.
 */
public class ReturnDemo {

    /**
     * try有return,finally也有return,以finally的为准
     * @return
     */
    public static int noException(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i--; //9
            return --i; //8
        }
        catch(Exception e){
            --i;
            System.out.println("i in catch - form try block is:"+i);
            return --i;
        }
        finally{
            System.out.println("i in finally - from try or catch block is:"+i); //8
            return ++i; //9 返回这个
        }
    }

    public static int tryCatchReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            return --i; //9
        }
        catch(Exception e){
            --i;
            System.out.println("i in catch - form try block is:"+i);
            return --i;
        }
        finally{
            System.out.println("i in finally - from try or catch block is:"+i); //9
            --i; //8
            System.out.println("i in finally block is:"+i); //8
        }
    }

    /**
     * finally的return的优先级最高
     * 但try/catch的return也会执行
     * @return
     */
    public static int tryCatchFinallyReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i = i/0;
            return --i;
        }
        catch(Exception e){
            System.out.println("i in catch - form try block is:"+i); //10
            --i; //9
            System.out.println("i in catch block is:"+i); //9
            return --i; //8
        }
        finally{
            System.out.println("i in finally - from try or catch block is--"+i); //8
            --i; //7
            System.out.println("i in finally block is--"+i); //7
            return --i; //6
        }
    }

    public static int tryExCatchReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i=i/0;
            return --i;
        }catch(Exception e){
            System.out.println("i in catch - form try block is:"+i); //10
            return --i; //9 返回这个
        }finally{

            System.out.println("i in finally - from try or catch block is:"+i); //9
            --i; //8
            System.out.println("i in finally block is:"+i); //8
        }
    }

    public static int tryExCatchExReturn() {
        int i = 10;
        try {
            System.out.println("i in try block is:" + i); //10
            i = i / 0;
            return --i;
        } catch (Exception e) {
            System.out.println("i in catch - form try block is:" + i); //10
            int j = i / 0; 
            return --i;
        } finally {

            System.out.println("i in finally - from try or catch block is:" + i); //10
            --i; //9
            --i; //8
            System.out.println("i in finally block is:" + i); //8
            return --i; //7 返回这个
        }
    }


    public static void main(String[] args){
        System.out.println("###" + noException()); //9
        System.out.println("###" + tryCatchReturn()); //7 
        System.out.println("###" + tryCatchFinallyReturn()); //6
        System.out.println("###" + tryExCatchReturn()); //9 
        System.out.println("###" + tryExCatchExReturn()); //7
    }
}
小结

如果finally中有return,则最后的返回以它为准,同时try或catch的return如果可以运行到,也会执行其表达式。

参考

关于Java中try-catch-finally-return的执行顺序

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65604.html

相关文章

  • try-catch-finally,被你忽略掉的执行顺序

    摘要:是捕捉异常的神器,不管是调试还是防止软件崩溃,都离不开它。今天笔者介绍一下加上后的执行顺序嗯按顺序执行了。现在笔者在语句块中故意报错看来,和的都需要先经过。 try-catch是捕捉异常的神器,不管是调试还是防止软件崩溃,都离不开它。今天笔者介绍一下加上finally后的执行顺序 function test() { try { console.log(1); } fin...

    bbbbbb 评论0 收藏0
  • try-catch-finally,被你忽略掉的执行顺序

    摘要:是捕捉异常的神器,不管是调试还是防止软件崩溃,都离不开它。今天笔者介绍一下加上后的执行顺序嗯按顺序执行了。现在笔者在语句块中故意报错看来,和的都需要先经过。 try-catch是捕捉异常的神器,不管是调试还是防止软件崩溃,都离不开它。今天笔者介绍一下加上finally后的执行顺序 function test() { try { console.log(1); } fin...

    浠ラ箍 评论0 收藏0
  • “崩溃了?不可能,我全 Catch 住了” | Java 异常处理

    摘要:允许存在多个,用于针对不同的异常做不同的处理。表示程序可能需要捕获并且处理的异常。因此,我们应该尽可能的避免通过异常来处理正常的逻辑检查,这样可以确保不会因为发生异常而导致性能问题。异常表中的每一条记录,都代表了一个异常处理器。 showImg(https://segmentfault.com/img/remote/1460000017918154?w=640&h=100); show...

    stdying 评论0 收藏0
  • 阿里小哥带你玩转JVM:揭秘try-catch-finally在JVM底层都干了些啥?

    摘要:当触发异常的字节码的索引值在某个异常表条目的监控范围内,虚拟机会判断所抛出的异常和该条目想要捕获的异常是否匹配。 作者:李瑞杰目前就职于阿里巴巴,狂热JVM爱好者让我们准备一个函数:showImg(https://user-gold-cdn.xitu.io/2019/5/19/16acbce35adfefb7);然后,反编译他的字节码:showImg(https://user-gold-cd...

    番茄西红柿 评论0 收藏0
  • 阿里小哥带你玩转JVM:揭秘try-catch-finally在JVM底层都干了些啥?

    摘要:当触发异常的字节码的索引值在某个异常表条目的监控范围内,虚拟机会判断所抛出的异常和该条目想要捕获的异常是否匹配。 作者:李瑞杰目前就职于阿里巴巴,狂热JVM爱好者让我们准备一个函数:showImg(https://user-gold-cdn.xitu.io/2019/5/19/16acbce35adfefb7);然后,反编译他的字节码:showImg(https://user-gold-cd...

    番茄西红柿 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<