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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ehuashao 中级黑马   /  2014-3-29 10:38  /  1036 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?看了老师的视频感觉不是很懂啊。那一些数据库关闭资源的一般都写在finally里,不执行怎么办。

1 个回复

倒序浏览
楼主要是不明白的话,可以做个测试。。
在try中写了return,后面又写了finally,
* 是先执行return还是先执行fianlly?
* @author hjl
*
*答:是先执行return
  1. public class test6 {

  2.         public static int test(){
  3.                 try {
  4.                         return function1();
  5.                 }finally{
  6.                         return function2();
  7.                 }
  8.         }
  9.         public static int function1(){
  10.                 System.out.println("function1执行了");
  11.                 return 1;
  12.         }
  13.         public static int  function2(){
  14.                 System.out.println("function2执行了");
  15.                 return 2;
  16.         }
  17.         
  18.         public  static void main(String[] args){
  19.                 System.out.println(new test6().test());
  20.         }
  21. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马