A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© outsider1020 中级黑马   /  2014-12-23 10:50  /  895 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//在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;
                }
         }

}
finally语句块被执行!
10

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马