GVKun编程网logo

JAVA中try、catch、finally带return的执行顺序总结(java中try catch finally执行顺序)

21

本文的目的是介绍JAVA中try、catch、finally带return的执行顺序总结的详细情况,特别关注java中trycatchfinally执行顺序的相关信息。我们将通过专业的研究、有关数据的

本文的目的是介绍JAVA中try、catch、finally带return的执行顺序总结的详细情况,特别关注java中try catch finally执行顺序的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解JAVA中try、catch、finally带return的执行顺序总结的机会,同时也不会遗漏关于asp.net中try catch finally中含有return时的执行顺序、java try catch finally的执行顺序、Java 中 try、catch、finally 语句块的执行顺序、java 中关于 try、catch、finally 中的细节分析的知识。

本文目录一览:

JAVA中try、catch、finally带return的执行顺序总结(java中try catch finally执行顺序)

JAVA中try、catch、finally带return的执行顺序总结(java中try catch finally执行顺序)

  异常处理中,try、catch、finally的执行顺序,大家都知道是按顺序执行的。即,如果try中没有异常,则顺序为try→finally,如果try中有异常,则顺序为try→catch→finally。但是当try、catch、finally中加入return之后,就会有几种不同的情况出现,下面分别来说明一下。也可以跳到最后直接看总结。

 

一、try中带有return

 1     private int testReturn1() {
 2         int i = 1;
 3         try {
 4             i++;
 5             System.out.println("try:" + i);
 6             return i;
 7         } catch (Exception e) {
 8             i++;
 9             System.out.println("catch:" + i);
10         } finally {
11             i++;
12             System.out.println("finally:" + i);
13         }
14         return i;
15     }

输出:

try:2
finally:3
2

  因为当try中带有return时,会先执行return前的代码,然后暂时保存需要return的信息,再执行finally中的代码,最后再通过return返回之前保存的信息。所以,这里方法返回的值是try中计算后的2,而非finally中计算后的3。但有一点需要注意,再看另外一个例子:

 1     private List<Integer> testReturn2() {
 2         List<Integer> list = new ArrayList<>();
 3         try {
 4             list.add(1);
 5             System.out.println("try:" + list);
 6             return list;
 7         } catch (Exception e) {
 8             list.add(2);
 9             System.out.println("catch:" + list);
10         } finally {
11             list.add(3);
12             System.out.println("finally:" + list);
13         }
14         return list;
15     }

输出:

try:[1]
finally:[1, 3]
[1, 3]

  看完这个例子,可能会发现问题,刚提到return时会临时保存需要返回的信息,不受finally中的影响,为什么这里会有变化?其实问题出在参数类型上,上一个例子用的是基本类型,这里用的引用类型。list里存的不是变量本身,而是变量的地址,所以当finally通过地址改变了变量,还是会影响方法返回值的。

 

二、catch中带有return

 1     private int testReturn3() {
 2         int i = 1;
 3         try {
 4             i++;
 5             System.out.println("try:" + i);
 6             int x = i / 0 ;
 7         } catch (Exception e) {
 8             i++;
 9             System.out.println("catch:" + i);
10             return i;
11         } finally {
12             i++;
13             System.out.println("finally:" + i);
14         }
15         return i;
16     }

输出:

try:2
catch:3
finally:4
3

  catch中return与try中一样,会先执行return前的代码,然后暂时保存需要return的信息,再执行finally中的代码,最后再通过return返回之前保存的信息。所以,这里方法返回的值是try、catch中累积计算后的3,而非finally中计算后的4。

 

三、finally中带有return

 1     private int testReturn4() {
 2         int i = 1;
 3         try {
 4             i++;
 5             System.out.println("try:" + i);
 6             return i;
 7         } catch (Exception e) {
 8             i++;
 9             System.out.println("catch:" + i);
10             return i;
11         } finally {
12             i++;
13             System.out.println("finally:" + i);
14             return i;
15         }
16     }

输出:

try:2
finally:3
3

  当finally中有return的时候,try中的return会失效,在执行完finally的return之后,就不会再执行try中的return。这种写法,编译是可以编译通过的,但是编译器会给予警告,所以不推荐在finally中写return,这会破坏程序的完整性,而且一旦finally里出现异常,会导致catch中的异常被覆盖。

 

总结:

1、finally中的代码总会被执行。

2、当try、catch中有return时,也会执行finally。return的时候,要注意返回值的类型,是否受到finally中代码的影响。

3、finally中有return时,会直接在finally中退出,导致try、catch中的return失效。

 

asp.net中try catch finally中含有return时的执行顺序

asp.net中try catch finally中含有return时的执行顺序

1、try{} catch(){}finally{} return;


显然程序按顺序执行。


2、try{ return; }catch(){} finally{} return;


(1)、程序执行try块中return之前(包括return语句中的表达式运算)代码;

(2)、再执行finally块,最后执行try中return;

(3)、finally块之后的语句return,因为程序在try中已经return所以不再执行。


3、try{ } catch(){return;} finally{} return;


(1)、程序先执行try,如果遇到异常执行catch块,
(2)、有异常:则执行catch中return之前(包括return语句中的表达式运算)代码,再执行finally语句中全部代码,最后执行catch块中return. finally之后也就是4处的代码不再执行。
(3)、无异常:执行完try再finally再return.


4、try{ return; }catch(){} finally{return;}


(1)、程序执行try块中return之前(包括return语句中的表达式运算)代码;
(2)、再执行finally块,因为finally块中有return所以提前退出。


5、try{} catch(){return;}finally{return;}


(1)、程序执行catch块中return之前(包括return语句中的表达式运算)代码;
(2)、再执行finally块,因为finally块中有return所以提前退出。


6、try{ return;}catch(){return;} finally{return;}


(1)、程序执行try块中return之前(包括return语句中的表达式运算)代码;
(2)、有异常:执行catch块中return之前(包括return语句中的表达式运算)代码;则再执行finally块,因为finally块中有return所以提前退出。
(3)、无异常:则再执行finally块,因为finally块中有return所以提前退出。
 

 

参考资料: try中finally与return   http://www.studyofnet.com/news/1018.html


java try catch finally的执行顺序

java try catch finally的执行顺序

class DivTest
{
  int div(int a, int b) throws Exception
  {
    return a/b;
  }
}
class FinallyDemo
{
  public static void main(String[] args)
  {
    DivTest d = new DivTest();
    try
    {
      int x = d.div(4,0);
      System.out.println("x="+x);       
    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
      return;  //这里return,仍然要执行finlly,之后不继续向下执行
      System.exit(0);//推出JVM,此时不能读finally,只有这一种情况
    }
    finally    //主要用来关闭资源
    {
      System.out.println("Finally");
    }
    System.out.println("Hello world!");
  }
}

Java 中 try、catch、finally 语句块的执行顺序

Java 中 try、catch、finally 语句块的执行顺序

  • 假设代码顺序书写如下:try → catch → finally → 其他代码
  • 则:
    • 1、正常执行顺序:try → catch → finally → 其他代码
    • 2、try,catch和finally和其他代码的执行条件:
      • 先执行try。
      • 只要try中有异常,catch就会执行;
      • finally总会执行;
      • 其他代码:
        • 如果finally中没有return:其他代码会执行;
        • 如果finally中有return:其他代码不会执行;
    • 3、方法的返回结果确定:
      • finally 有return:(finally中的return)
        • 返回结果为 finally 中的值,try(+catch,+ 其他代码)的 return 值会被覆盖掉。
          注:有return,其他代码不会执行。所以finally就是最终执行的代码。故返回结果为 finally 中的值。
      • finally 没有return:(正常执行顺序返回结果)
        • 返回结果就是正常代码执行顺序的返回结果。try(+catch,+ 其他代码)的 return 会被寄存起来。一般 finally 不会修改返回值,除非 finally 修改了该引用指向的实际内存内容。
          注:没有return,其他代码会执行,finally不是最终执行的代码。返回结果为正常执行顺序的返回结果。
  • 参考:
    • java 的异常、finally 和 return - wthfeng 的专栏 - CSDN 博客 https://blog.csdn.net/wthfeng/article/details/89329161
    • Java 中 try、catch、finally 语句块的执行顺序 - 换了马甲的小强的博客 - CSDN 博客 https://blog.csdn.net/cockroach02/article/details/80186723

java 中关于 try、catch、finally 中的细节分析

java 中关于 try、catch、finally 中的细节分析

Demo1

public class Demo1 {
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            // result = "catch";
            t = "catch";
            return t;
        } finally {
            t = "finally";
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo1.test());
    }
}

Demo2

public class Demo2 {
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            // result = "catch";
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo2.test());
    }

Demo3

public class Demo3 {
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            // System.out.println(t);
            // return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo3.test());
    }

}

Demo4

public class Demo4 {
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo4.test());
    }
}

Demo5

/**
 * java.lang.NumberFormatException: null
 */
public class Demo5 {

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            Integer.parseInt(null);
            return t;
        } finally {
            t = "finally";
            //return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo5.test());
    }
}

Demo6

public class Demo6 {
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            Integer.parseInt(null);
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo6.test());
    }

}

Demo7

/**
 * java.lang.NumberFormatException: null
 */
public class Demo7 {

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo7.test());
    }
}

Demo8

public class Demo8 {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo8.test());
    }
}

Demo9

public class Demo9 {
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            String.valueOf(null);
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo9.test());
    }
}

对以上所有的例子进行总结

  • 1 try、catch、finally 语句中,在如果 try 语句有 return 语句,则返回的之后当前 try 中变量此时对应的值,此后对变量做任何的修改,都不影响 try 中 return 的返回值
  • 2 如果 finally 块中有 return 语句,则返回 try 或 catch 中的返回语句忽略。
  • 3 如果 finally 块中抛出异常,则整个 try、catch、finally 块中抛出异常

所以使用 try、catch、finally 语句块中需要注意的是

  • 1 尽量在 try 或者 catch 中使用 return 语句。通过 finally 块中达到对 try 或者 catch 返回值修改是不可行的。
  • 2 finally 块中避免使用 return 语句,因为 finally 块中如果使用 return 语句,会显示的消化掉 try、catch 块中的异常信息,屏蔽了错误的发生
  • 3 finally 块中避免再次抛出异常,否则整个包含 try 语句块的方法回抛出异常,并且会消化掉 try、catch 块中的异常

关于JAVA中try、catch、finally带return的执行顺序总结java中try catch finally执行顺序的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于asp.net中try catch finally中含有return时的执行顺序、java try catch finally的执行顺序、Java 中 try、catch、finally 语句块的执行顺序、java 中关于 try、catch、finally 中的细节分析的相关知识,请在本站寻找。

本文标签: