捕捉多个异常并抛出异常

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

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,则返回一个列表作为例外。

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);

}

}

回答:

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

​​

回答:

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

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

**/

}

}

}

以上是 捕捉多个异常并抛出异常 的全部内容, 来源链接: utcz.com/qa/267161.html

回到顶部