CompletableFuture检查exception

使用Java 8强大functionCompletableFuture,我想使用此新function的exception转换旧的异步代码。 但是经过检查的例外情况令我困扰。 这是我的代码。

 CompletableFuture asyncTaskCompletableFuture = CompletableFuture.supplyAsync(t -> processor.process(taskParam)); 

process方法的签名:

 public void process(Message msg) throws MyException; 

如何处理ComletableFuture中的已检查exception?

我试过这种方式,但我不知道这是否是解决问题的好方法。

 @FunctionalInterface public interface RiskEngineFuncMessageProcessor extends Supplier { @Override default Void get() { try { return acceptThrows(); } catch (final Exception e) { throw new RuntimeException(e); } } Void acceptThrows() throws Exception; 

使用Supplier的FunctionalInterface,我可以包装exception:

 final MyFuncProcessor func = () -> { processor.process(taskParam); return null; }; CompletableFuture asyncTaskCompletableFuture = CompletableFuture.supplyAsync(func) .thenAccept(act -> { finishTask(); }) .exceptionally(exp -> { log.error("Failed to consume task", exp); failTask( exp.getMessage()); return null; });