finally的一个妙用

●传统用法
try-catch-finally大家都会用:try包裹可能抛出异常的代码;catch捕获异常并对其处理;finally做一些资源关闭等回收工作。简单明了一句话就能说清。

稍微进阶一些的,大家可能在笔试/面试/工作中遇到,如果try、catch中有了return,那finally的执行时在return之前还是return之后呢?我们查看java官方的说明可以得到答案:

If the try clause executes a return, the compiled code does the following:

Saves the return value (if any) in a local variable.
Executes a jsr to the code for the finally clause.
Upon return from the finally clause, returns the value saved in the local variable.
翻译一下:

如果try块中有return,代码会做以下工作:

1、将return的值保存在一个本地变量中;

2、执行finally块中的Java Specification Requests(jsr,java标准提案)代码;

3、执行完finally块后(从finally中返回后),return刚才保存在本地变量中的值。

解读一下,我们能从中得出以下结论:

1、try、catch和finally中都可以有return;

2、try或者catch(二者层次相同,正常逻辑与异常逻辑,因此可以如此推断,当然也可以验证)中return的值会先保存起来;

3、finally中执行完代码后(此处jsr笔者不是很理解,暂视为正常的java代码),无论对第2步中的返回值是否有修改,返回的均是第2步保存的内容。

 

●特殊妙用
根据前一部分的知识,我们可以进行如下的使用。结合这么一个需求:一个函数返回某个集合,并把该集合清空,以备其他操作该集合的对象能重新写入数据。一般业务场景可能是需要定期读取集合/内存/缓存中的数据,并把读过的数据删除,已防止集合/内存/缓存撑爆。

如果对finnaly不是很熟悉,可能会写出这样的代码:

public class Demo {
  public static final List<String> stringList = new ArrayList<>();
 
  public List<String> getAndClear(){
    List<String> result = new ArrayList<>(stringList);
    stringList.clear();
    return result;
  }
}
因为无法在return之后执行代码,因此考虑生成一个新的集合,把原集合清空,返回新集合。这么做不优雅之处在于手动实例化了一个新的对象。

但如果我们用finally来做,就不需要自己去实例化新对象了:

public class Demo {
  public static final List<String> stringList = new ArrayList<>();
 
  public List<String> getAndClear(){
   try{
     return stringList;
   }
   finally {
     stringList.clear();
   }
  }
}
以上就是finnaly的一个妙用,主要是理解try-catch中用了return,代码的执行顺序已经数据变更。

posted @ 2019-06-03 16:12  那些年的代码  阅读(385)  评论(0编辑  收藏  举报