开发者

Exception in Merging 2 List

I have merged 2 List and able to get data.

But when there is any exception in List1 then other List2 also does not get displayed.

List newList = new Arr开发者_Python百科ayList();
newList.addAll(listOne);
newList.addAll(listTwo);

So I need to display at least 1 list even if the other list has an Exception.


Although you should probably check why adding the elements fails and resolve that, a way to solve your problem is the following:

public void addAllIgnoreException(List baseList, List listToBeAdded){
  try {
    baseList.addAll(listToBeAdded);
  } catch (Exception e) {
    //log the exception
  }  
}

You can use this method to add the elements of 'listToBeAdded' to 'baseList' without having any exceptions:

List newList = new ArrayList();
addAllIgnoreException(newList, listOne);
addAllIgnoreException(newList, listTwo);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜