开发者社区> 问答> 正文

无法在Lambda表达式内触发异常

我是Java 8的新手,并尝试在lambda表达式中引发异常,如下所示:如果subQty小于min或大于max,在这种情况下,我的单元测试将min / max计算为182和255,并且我提交的subQty为10,因此它应该引发异常并且未通过此单元测试。但是,我一直在开绿灯,为什么呢?

public void verifyingIndividualSubmissionValueLimits(String alias, double min, double max)
    // ... some code here ...

    // Get local stock price
    CompletableFuture<Double> localPriceFuture = instrumentRequester.getMidPrice(instId);

    // Calculate the min and max quantity
    localPriceFuture.thenAcceptBoth(dollarFxRateFuture,
            (localPrice, fxRate) -> {
                double minLocalValue = min * fxRate;
                double maxLocalValue = max * fxRate;
                long minQuantity = Math.round(minLocalValue / localPrice);
                long maxQuantity = Math.round(maxLocalValue / localPrice);
                if (subQty < minQuantity || subQty > maxQuantity) {
                    log.debug("We should throw an exception because subQty is {}", subQty);
                    throw new SubmissionValidationException(String.format("Quantity: %s, is not within %s and %s", subQty, minQuantity, maxQuantity));
                }
            }
    );
}

展开
收起
垚tutu 2019-12-19 16:36:26 617 0
1 条回答
写回答
取消 提交回答
  • #include

    您在其他线程中引发异常。您正在创建一个计算最小,最大速率并引发异常的线程,但是线程中发生异常,因此您无法在主线程中看到任何异常(在这种情况下verifyingIndividualSubmissionValueLimits)。您可以在这里阅读回调和异步线程

    2019-12-19 16:36:33
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载