全网最简洁明了的try catch finally return讲解!!

目录

前言

导语

情况1-----无return无finally

情况2-----无return有finally

情况3-----if或catch有return,finally中无return

情况4-----if和catch中有return,finally中无return;

情况5-----if,catch,finally中都有return

结论(重点)


 

前言

        很久没更新了,因为博主最近这个阶段学习的知识较多,需要时间消化。这里给大家讲下异常处理中的try catch finally return;不懂异常的小伙伴可以先收藏,博主这个阶段学习完成后将会给大家讲讲异常等内容

导语

       作为一名非科班出身的无基础java初学者。我会在这里记录我的学习过程及心得分享,希望会对你们想要入行的小伙伴有所帮助,多一个参考的点。

       其次希望我的分享能对同样是初学者的你能有所帮助。

       同时我也想以此激励自己学习,如果有志同道合的小伙伴就最好啦。大家一起进步!

        最后,若您有自己的想法或者觉得我的讲述有问题,需要补充或改正的,欢迎在下方留言互相讨论!!

情况1-----无return无finally

        代码演示:

 public static void main(String[] args) {
        method();
    }
    public static void method(){
        try {
            System.out.println("throw前的语句被使用");
            throw new Exception();
            //System.out.println("throw后的语句");
        } catch (Exception e) {
            System.out.println("catch中的方法被调用" );
        }
    }

      运行结果:
                throw前的语句被使用
                catch中的方法被调用

 

情况2-----无return有finally

        代码演示:

 public static void main(String[] args) {
        method();
    }

    public static void method() {
        try {
            System.out.println("throw前的语句");
            throw new Exception();
            //System.out.println("throw后的语句");
        } catch (Exception e) {
            System.out.println("catch中的方法被调用");
        }
        finally {
            System.out.println("finally中的内容被调用");
        }
    }

 

        运行结果:

                throw前的语句被使用

                catch中的方法被调用

                finally中的内容被调用

情况3-----if或catch有return,finally中无return

       代码演示:(演示其中一种情况)
 

  public static void main(String[] args) {
        int i =method();
        System.out.println(i);
    }

    public static int method() {
        try {
            return 1;
        } catch (Exception e) {
          
        }
        finally {

        }
            return2;
    }

        运行结果:

                1

情况4-----if和catch中有return,finally中无return;

        代码演示:

public static void main(String[] args) {
        int i =method();
        System.out.println(i);
    }

    public static int method() {
        try {
            return 1;
        } catch (Exception e) {
            return 2;
        }
        finally {

        }

    }

       运行结果:

                1

情况5-----if,catch,finally中都有return

        代码演示:
 

   public static void main(String[] args) {
        int i =method();
        System.out.println(i);
    }

    public static int method() {
        try {
            return 1;
        } catch (Exception e) {
            return 2;
        }
        finally {
            return 3;
        }

    }

        运行结果:

                3

结论(重点)

        1.try-->catch-->finally       无论什么情况下,finally都会执行。
        2.try或catch有return情况下,在原来的顺序下,遇到return时先把return后的表达式进               行计算并储存起来,而后执行finally中的语句。若finally中有对return后的值进行修改               的 操作,如果该值是基本数据类型,则储存起来的值不会被修改,如果是引用类型就             会被修改,最后            finally执行完毕后,再去执行return,退出语句体。
        3.finally中有return,则无论前面是否有return,都只会返回finally中return后的值。
        4.不要在finally块中使用return。

        5.throw后不能跟语句,即抛出异常后,不能跟其他任何语句,会报错"Unreachable                   statement"


版权声明:本文为yueseck原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。