我有这个方法:
@Async
@Override
public CompletableFuture<List<ProductDTO>> dashboard( ) throws GeneralException {
List<Product> products = newArrayList();
/*....
....*/
//I want this exception when calling CompletableFuture#get()
if ( products.isEmpty() ) {
throw new GeneralException( "user.not-has.product-message",
"user.not-has.product-title" );
}
return CompletableFuture
.completedFuture( ...) );
}
并且GeneralException
是这样定义的:
public class GeneralException extends RuntimeException {...}
问题是,什么时候GeneralException
抛出,当我调用CompletableFuture#get()
来获取我的数据或异常时,我有 ajava.util.concurrent.ExecutionException
而不是我的 custom GeneralException
。Spring doc 声称:
当
@Async
方法具有Future
-typed 返回值时,很容易管理在方法执行期间引发的异常,因为在调用结果时会引发此get
异常Future
。
我究竟做错了什么?非常感谢
编辑: 这是客户端代码:
public static <T> T retrieveDataFromCompletableFuture( @NotNull CompletableFuture<T> futureData ) {
T data = null;
try {
data = futureData.get();
} catch ( Exception e ) {
log.error( "Can't get data ", e );
}
return data;
}
还有一个例外:
java.util.concurrent.ExecutionException: org.app.exceptions.GeneralException: user.not-has.product-message
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
.....
Caused by: org.app.exceptions.GeneralException: user.not-has.product-message
为什么我还有java.util.concurrent.ExecutionException
?