2016-04-30 1223 views
1

请看下面的代码的新列表:捕捉多个异常并抛出异常

public void editProduct(String articleNumber, ...) { 
product.setArticleNumber(articleNumber); 
product.setDescription(description); 
product.setLendable(lendable); 
product.setName(name); 
product.setPrice(price); 
product.setPlace(place); 
} 


所有制定者可以抛出一个自定义消息ProductException。 但我想抓住所有的例外。如果有例外情况,我想向所有错误的GUI发送一个列表。

我怎么能做到这一点,或者我需要围绕一个尝试捕捉每一行?

+0

如果发生异常,为什么要继续尝试更改产品?如果发生异常,则表示存在问题;并且'product'应该保留在开始编辑它之前的状态(* Effective Java *指这是[“failure atomicity”](http://stackoverflow.com/questions/29842845/what-是故障影响的原子使用的逐J-布洛赫和 - 如何-其益合-的组术语ⅰ))。 –

+0

您好,我想向用户显示不同的错误:“ - 没有描述, - 没有价格 - ......” – Demian

+0

什么是例外这里为你的产品是空的一些方法参数?为空?这些方法都是简单的setter?这里怎么可能会出现异常... – GOXR3PLUS

回答

0

,如果您需要在列表中的所有例外,那么你必须围绕通过尝试捕捉每一行,并添加例外列表中,并在最后的检查列表的大小大于0,则返回一个列表作为例外。

List<CustomException> exceptionList = new ArrayList<CustomException>(); 
public void editProduct(String articleNumber, ...) 
{ 
    try{ 
      product.setArticleNumber(articleNumber); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
     try{ 
     product.setDescription(description); 
     }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
    try{ 
    product.setLendable(lendable); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
    try{ 
      product.setName(name); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
     try{ 
     product.setPrice(price); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
    try{ 
     product.setPlace(place); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
} 
+0

谢谢你,但有没有这个更好的解决方案? – Demian

+0

而不是从setter方法抛出异常,你可以添加一个验证方法来处理你的所有setter检查,并在那里执行你所有的操作validations.that将是连击方法。 –

+0

嗨,这是一种可能性。但是我想保留domainclass中的所有“域”代码。 – Demian

0

如果你真的想要做这样的事情,你可以委托属性设置为会赶上运行二传手作为拉姆达的方法,再搭上可能是个例外,或将其添加到列表:

​​
1

哟可以尝试下面的代码。如果合适

public void editProduct(String articleNumber, ...) { 
    int count=1; 
    Vector<String> v=new Vector<String>(); 
    while(count<=6) 
    { 
     try{ 

      switch(count) 
      { 
       case 1:product.setArticleNumber(articleNumber);break;//assume it will throw ArticalException 
       case 2:product.setDescription(description);break; //DescriptionException 
       case 3:product.setLendable(lendable);break;   //LendableException 
       case 4:product.setName(name);break;     //NameException 
       case 5:product.setPrice(price);break;    //PriceException 
       case 6:product.setPlace(place);break;    //PlaceException 
      } 
      count++; 
     }catch(Exception e) 
     { 
      v.add(e.getMessage); 
      count++; 
      /* 
      *suppose there is some exception like ArticalException,PriceException 
      *then it will store in vector and your program running continue 
      *and at last you have the list of all exceptions that are occured in program 
      *now you will take desired action what you want to do 
      **/ 
     } 
    } 

} 
+0

嗨,我喜欢你的解决方案!我不知道这是最佳做法 一件小事,如果二传手失败了,计数仍然是一样的。所以你有一个无限循环。 也许你可以在开关之前或最后一组中增加? – Demian

+0

看我代码..有一个计数++ statement.there为count ++语句之后开关和catch块 –

+0

是啊...但'为(计数= 1;计数<= 6;计数++)'做同样的事情,并且它更可读。此外,你应该赶上'ProductException'。捕捉“Exception”是一个糟糕的主意,因为它会导致意外的异常作为验证错误报告给用户。 –