Java的finally语句在try或catch中的return语句执行之后还是之前?
import java.util.HashMap; import java.util.Map; public class FinallyDemo1 { public static void main(String[] args) { System.out.println("================================="); System.out.println("A情况:finally在try的return执行之后,返回之前。"); System.out.println("main\ti="+test1()); System.out.println("================================="); System.out.println("B情况:finally在try的return执行之后,返回之前。finally的return将覆盖try的return:"); System.out.println("main\ti="+test2()); System.out.println("================================="); System.out.println("异常时候,try的return不执行。catch和finally的执行情况与try和finally的执行情况一样。如A情况:"); System.out.println("main\ti="+test3()); System.out.println("================================="); System.out.println("异常时候,try的return不执行。catch和finally的执行情况与try和finally的执行情况一样。如B情况:"); System.out.println("main\ti="+test4()); System.out.println("================================="); System.out.println("地址传递的参数,在try-finally中将覆盖返回值。"); System.out.println("main\tmap="+test5()); System.out.println("================================="); System.out.println("地址传递的参数,在try-finally中将覆盖返回值。"); System.out.println("main\tmap="+test6()); System.out.println("================================="); } /** * finally代码块是在try代码块中的return语句执行之后,返回之前执行的。 * @return */ public static int test1(){ int i=1; try { return i+=100; } catch (Exception e) { System.out.println("catch"); }finally{ i++; System.out.println("finally\ti="+i); } return i; } /** * finally代码块中的return语句覆盖try代码块中的return语句 * @return */ @SuppressWarnings("finally") public static int test2(){ int i=1; try { return i+=100; } catch (Exception e) { System.out.println("catch"); }finally{ i++; System.out.println("finally\ti="+i); return i; } } /** * try代码块中的return语句在异常的情况下不会被执行; * catch中的return执行情况与未发生异常时try中return的执行情况完全一样。 * @return */ @SuppressWarnings("finally") public static int test3(){ int i=1; try { i=i/0; return i+=100; } catch (Exception e) { System.out.println("catch"); return i+=100; }finally{ i++; System.out.println("finally\ti="+i); } } @SuppressWarnings("finally") public static int test4(){ int i=1; try { i=i/0; return i+=100; } catch (Exception e) { System.out.println("catch"); return i+=100; }finally{ i++; System.out.println("finally\ti="+i); return i; } } /** * 值传递:返回值不变 * 地址传递:返回值会变 * @return */ public static Map test5(){ Map<String, String> map = new HashMap<String, String>(); map.put("param1", "初始化值"); try { System.out.println("try"); map.put("param1", "try值"); return map; } catch (Exception e) { System.out.println("catch"); map.put("param1", "catch值"); } finally { System.out.println("finally"); map.put("param1", "finally值"); map = null; } return map; } @SuppressWarnings("finally") public static Map test6(){ Map<String, String> map = new HashMap<String, String>(); map.put("param1", "初始化值"); try { System.out.println("try"); map.put("param1", "try值"); return map; } catch (Exception e) { System.out.println("catch"); map.put("param1", "catch值"); } finally { System.out.println("finally"); map.put("param1", "finally值"); map = null; return map; } } }
控制台:
=================================
A情况:finally在try的return执行之后,返回之前。
finally i=102
main i=101
=================================
B情况:finally在try的return执行之后,返回之前。finally的return将覆盖try的return:
finally i=102
main i=102
=================================
异常时候,try的return不执行。catch和finally的执行情况与try和finally的执行情况一样。如A情况:
catch
finally i=102
main i=101
=================================
异常时候,try的return不执行。catch和finally的执行情况与try和finally的执行情况一样。如B情况:
catch
finally i=102
main i=102
=================================
地址传递的参数,在try-finally中将覆盖返回值。
try
finally
main map={param1=finally值}
=================================
地址传递的参数,在try-finally中将覆盖返回值。
try
finally
main map=null
=================================