//在try语句块中有return语句,并且正常执行,则仍旧会正常的执行finally语句块。
public class FinallyDemo { public static void main(String [] args ) { System .out. println(method (10, 5));
} private static int method(int a, int b) { try { int c =a/ b; return c; } catch (Exception e) { e .printStackTrace() ; }finally{ System .out.println ("finally语句块被执行!");
} return - 1; }
} finally语句块被执行! 2
//如果finally语句块中有return 语句,则直接执行了return语句,就不再去之前try块中的return的语句了 public class FinallyDemo { public static void main(String [] args ) { System .out. println(method (10, 5));
} private static int method(int a, int b) { try { int c =a/ b; return c; } catch (Exception e) { e .printStackTrace() ; }finally{ System .out.println ("finally语句块被执行!"); return 10; } }
}
|