Function接口中的异常处理 [英] Exception handling in Function interface

查看:595
本文介绍了Function接口中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java 8中的新功能。这个问题与使用功能界面抽象公共代码的问题。从中得到的想法我写的如下:

I am new to Function interface in Java 8. This question is related to Issue in abstracting common code using function interface. Getting idea from this I have written like below:

  public void act(Input1 input) throws NonRetriableException, InvalidInputException {

    Function<UpdateTaskInput, Boolean> func = item -> {
        try {
            activityManager.update(item);
            return true;
        } catch (InterruptedException | JSONException e) {

            throw new NonRetriableException(e);
        } catch (LockUnavailableException e) {

            throw new NonRetriableException(e);
        }
    };

    try {
        lockManager.executeWithLock(input.getTaskID(), input, func);
    } catch (LockUnavailableException e) {
        log.error("{}",e);
        throw new NonRetriableException(e);
    }
   }

和:

 public void perform()
    throws AutoAllocationException {

     Function<UpdateTaskInput, Boolean> func = item -> {
        try {
            activityManager.allocateTask(item);
            return true;
        } catch (AutoAllocationException ex) {
            log.error("{}",ex);
        }
        return false;
    };

    try {
        lockManager.executeWithLock(input.getTaskID(), input, func);
    } catch (LockUnavailableException e) {
        log.error("{}",e);
    }
  }

LockManager中的executeWithLock()如下:

executeWithLock() in LockManager is as follows:

 @Override
 public <T,R> R executeWithLock(String lockName, T input, Function<T,R> func) throws LockUnavailableException {
    LockItem lockItem = acquireLock(lockName);
    R output = func.apply(input);
    releaseLock(lockItem); 
    return output;
}

现在我的问题是:

executeWithLock()函数中我是否必须捕获所有异常,我的意思是我应该从 func.apply()<处理异常/ code>。我想知道这个,因为, releaseLock 调用无论事件是否发生,我都应该这样做。

In executeWithLock() function do I have to capture all the exceptions, I mean should I handle exception from func.apply(). I want to know this because, releaseLock call I should do always irrespective of the fact exception has occurred or not.

推荐答案

如果 act 抛出异常,您的 LockItem lockItem 将不会被释放 executeWithLock 。因此,推荐使用try-finally语句时使用锁定 s:

If act throws an exception your LockItem lockItem will not be released in executeWithLock. Therefore it is recommended to used the try-finally statement when working with Locks:

public <T, R> R executeWithLock(String lockName, T input, Function<T, R> func) throws LockUnavailableException {
    LockItem lockItem = acquireLock(lockName);
    try  {
        R output = func.apply(input);
        return output;
    } finally {
        releaseLock(lockItem);
    }
}

另一点:

act 函数中抛出 NonRetriableException 声明任何异常暗示 NonRetriableException扩展RuntimeException 。但是下面的try-catch-block只捕获 LockUnavailableException 类型的异常。类型 NonRetriableException 的异常将不会被捕获,因此不会被处理(记录)。它们将传播给 act 的调用者,即 executeWithLock 。这再次显示了第一点的重要性。

Another point:
Since act is throwing a NonRetriableException in a Function which does not declare any Exception that implies NonRetriableException extends RuntimeException. But the following try-catch-block does only catch exceptions of type LockUnavailableException. Exceptions of type NonRetriableException will not be catched and therefore not handled (logged). They will be propagated to the caller of act which is executeWithLock. This shows again the significance of the first point.

这篇关于Function接口中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆