2017-08-07 128 views
-1

这里是我的代码:如何在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。有人可以指导我吗?

+1

*为什么*你不想使用另一个try/catch?这是做这件事的明显方式。 –

+0

我对此不太确定,但是我的老大让我不要使用它。还有其他的方式吗? –

+0

不是特别愉快,没有。 (我可能会提取一个子方法来使整个事情变得更清洁,但是......)如果你的老人添加了一个要求,不要用最明显的方法去做某件事,你应该问他们他们*希望你如何处理这个。 –

回答

4

我不想在此try catch中使用另一个try catch。

是的。

MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID()); 
try { 
    marketplaceBOObject.loadFromSable(); 
} catch (WhateverException e) { 
    // Do something here, or, if you prefer, add the exception to a list and process later 
    doSomething() ; 
    // Continue your loop above 
    continue ; 
} 
if (marketplaceBOObject.isActive()) { 

如果你真的不想这样做,你loadFromSable()方法可以返回一些对象,它提供有关呼叫的成功/失败的信息。但我不会推荐。

3

做到这样 - 这样你的代码的其余部分将运行不管有异常或不

 for (MerchantMarketplaceBO entity : merchantMarketplaceBOList) { 
      MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID()); 

      try{ 
       marketplaceBOObject.loadFromSable(); 
       if (marketplaceBOObject.isActive()) { 
        resultVector.add(marketplaceBOObject.getCodigoMarketplace()); 
       } 

      } 
      catch{ 
       if (marketplaceBOObject.isActive()) { 
        resultVector.add(marketplaceBOObject.getCodigoMarketplace()); 
       } 
      } 

     } 
+0

这不会在Java中编译 - 每个catch块*都有*指定一个异常。 (这里的重复肯定是不愉快的......) –

+0

哦,是的!我在代码中错过了这个“(Exception excep)”。无论他是否愿意,重复都取决于所有者。 –

0

您可以重构加载到捕获并返回异常,而不是一个单独的方法把它扔:

private Optional<Exception> tryLoadFromSable(MarketplaceBO marketplaceBOObject) { 
    try { 
     marketplaceBOObject.loadFromSable(); 
     return Optional.empty(); 
    } 
    catch(Exception e) { 
     return Optional.of(e); 
    } 
} 

然后你的循环中:

//inside for loop... 
MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID()); 
Optional<Exception> loadException = tryLoadFromSable(marketplaceBOObject); 
if(loadException.isPresent()) { 
    //Do something here, log it, save it in a list for later processing, etc. 
} 
+0

返回一个文字'NULL'并不是不好的建议! –

+1

@蒂莫西·卡特莱尔废话,在这种情况下与返回假相同 – Palamino

+0

至少我看到了一个很大的区别:一个(原始)布尔值永远不会引发NPE! –

0

另一个“绝招”对付那就是身体移动到循环到具有“额外” try/catch块单独的方法:

private MarketplaceBO loadFromSable(MerchantMarketplaceBO entity){ 
    MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID()); 
    try {   
     marketplaceBOObject.loadFromSable(); 
    } catch (WhateverException e) { 
     // do something to make marketplaceBOObject a valid object 
     // or at least log the exception 
    } 
    return marketplaceBOObject; 
} 

但是因为我们要坚持以抽象原则同层我们还需要将该方法的其他部分移至新的更小方法:

public void serveFromSableV2() { 
    String merchantCustomerID = ObfuscatedId.construct(request.getMerchantCustomerID()).getPublicEntityId(); 
    try { 
     List<MerchantMarketplaceBO> merchantMarketplaceBOList = 
      getAllMerchantMarketplacesBOsByMerchant(); 
     Vector<Marketplace> resultVector = new Vector<>(); 
     for (MerchantMarketplaceBO entity : merchantMarketplaceBOList) { 
      MarketplaceBO marketplaceBOObject = loadFromSable(entity); 
      addToActiveMarketplacesList(marketplaceBOObject,resultVector); 
     } 
     verifyHavingActiveMarketPlaces(resultVector); 
     setResponseWithWrapped(resultVector); 
    } catch (EntityNotFoundException | SignatureMismatchException | InvalidIDException e) { 
     throw new InvalidIDException("merch=" + merchantCustomerID + "[" + request.getMerchantCustomerID() + "]"); //C++ stack throws InvalidIDException if marketplace is not found in datastore 
    } 
}