这里是我的代码:如何在JAVA的try catch框中捕捉特定的行异常?
public void serveFromSableV2() {
String merchantCustomerID = ObfuscatedId.construct(request.getMerchantCustomerID()).getPublicEntityId();
try {
List<MerchantMarketplaceBO> merchantMarketplaceBOList = new MerchantMarketplaceBO(merchantCustomerID, null).getAllMerchantMarketplacesBOsByMerchant();
Vector<Marketplace> resultVector = new Vector<>();
for (MerchantMarketplaceBO entity : merchantMarketplaceBOList) {
MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID());
marketplaceBOObject.loadFromSable();
if (marketplaceBOObject.isActive()) {
resultVector.add(marketplaceBOObject.getCodigoMarketplace());
}
}
if (resultVector.isEmpty()) {
throw new InvalidIDException();
}
this.response = new getMarketplacesByMerchantIncludingInactiveResponse();
response.setWrapperValue(resultVector);
} catch (EntityNotFoundException | SignatureMismatchException | InvalidIDException e) {
throw new InvalidIDException("merch=" + merchantCustomerID + "[" + request.getMerchantCustomerID() + "]"); //C++ stack throws InvalidIDException if marketplace is not found in datastore
}
}
现在对于任何例外,它抛出我不想外界抓住它一个特定的行(marketplaceBOObject.loadFromSable();
),我想通过单独处理它再次继续我的循环。我不想在这个try catch中使用另一个try catch。有人可以指导我吗?
*为什么*你不想使用另一个try/catch?这是做这件事的明显方式。 –
我对此不太确定,但是我的老大让我不要使用它。还有其他的方式吗? –
不是特别愉快,没有。 (我可能会提取一个子方法来使整个事情变得更清洁,但是......)如果你的老人添加了一个要求,不要用最明显的方法去做某件事,你应该问他们他们*希望你如何处理这个。 –