使用者将尝试捕获逻辑封闭起来不起作用



我正在重构一些遗留代码,我遇到了这个函数:

private static void parseOptionalValues(Product product, Input source) {
try {
product.setProductType(...some operation with source...);
} catch (IllegalArgumentException ignored) {}
try {
product.setMaterial(...some operation with source...);
} catch (IllegalArgumentException ignored) {}
try {
product.setUnitPricingBaseMeasure(...some operation with source...);
} catch (IllegalArgumentException ignored) {}
try {
product.setUnitPricingMeasure(...some operation with source...);
} catch(IllegalArgumentException ignored){}
}

我的常识告诉我,为了保持"不要重复自己"原则,我应该包装这个 try-catch 逻辑,所以我引入了这个更改:

private static void parseOptionalValues(Product product, Input source) {
setOptionalParameter(...some operation with source..., product::setProductType);
setOptionalParameter(...some operation with source..., product::setMaterial);
setOptionalParameter(...some operation with source..., product::setUnitPricingBaseMeasure);
setOptionalParameter(...some operation with source..., product::setUnitPricingMeasure);
}
private static <T> void setOptionalParameter(T value, Consumer<T> consumer) {
try {
consumer.accept(value);
} catch (IllegalArgumentException ignored) {}
}

我正在运行一些单元测试和调试,但代码的行为不像以前那样,因为IllegalArgumentException没有被捕获而是引发,因此程序失败。

关于如何解决将 try-catch 逻辑封闭在一个地方的任何想法?

我认为问题可能是异常是由将参数传递给setter的代码引发的。因此,一种可能的方法是通过使用Supplier使这部分代码延迟,然后在try/catch块内的Supplier上调用.get()(以及调用Consumer(:

private static <T> void setOptionalParameter(
Supplier<? extends T> supplier, 
Consumer<? super T> consumer) {
try {
consumer.accept(supplier.get());
} catch (IllegalArgumentException ignored) {
}
}

可以按如下方式调用该方法:

setOptionalParameter(() -> ...some operation with source..., product::setProductType);

请注意,我改进了方法的签名,使其现在分别接受更广泛的泛型子类型和SupplierConsumer的超类型。


编辑:根据评论,上述方法可能不够灵活,即如果该方法接受多个参数等。在这种情况下,最好使用Runnable实例:

private static void setOptionalParameter(Runnable action) {
try {
action.run();
} catch (IllegalArgumentException ignored) {
}
}

调用现在将变为:

setOptionalParameter(() -> {
ProductType productType = ...some operation with source...;
Material material = ...some operation with source...;
product.doSomethingWith2Args(productType, material);
});

最新更新