[英]Catch multiple exceptions and throw a new list of exceptions
Look at the following code:看下面的代码:
public void editProduct(String articleNumber, ...) {
product.setArticleNumber(articleNumber);
product.setDescription(description);
product.setLendable(lendable);
product.setName(name);
product.setPrice(price);
product.setPlace(place);
}
All of the setters can throw a ProductException with a custom message.所有的 setter 都可以抛出带有自定义消息的 ProductException。 But I want to catch all the exceptions.
但我想捕获所有异常。 If there are exceptions, I want to send a list to the GUI of all the errors.
如果有异常,我想将所有错误的列表发送到 GUI。
How can I do this, or do I need to surround every line with a Try Catch?我该怎么做,或者我是否需要用 Try Catch 包围每一行?
Yo can try the following code.你可以试试下面的代码。 if it is suitable
如果合适
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
**/
}
}
}
if you need all the exceptions in list then you have to surround each line by try catch and add the exception in a list and in the last check the size of the list is greater than 0 then return that list as exception.如果您需要列表中的所有异常,那么您必须通过 try catch 包围每一行并将异常添加到列表中,并在最后一次检查列表的大小是否大于 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);
}
}
If you really want to do such a thing, you could delegate the property setting to a method that would catch run the setter as a lambda, then catch the possible exception, or add it to a list:如果你真的想做这样的事情,你可以将属性设置委托给一个方法,该方法可以将 setter 作为 lambda 捕获运行,然后捕获可能的异常,或者将其添加到列表中:
class C {
@FunctionalInterface
private static interface TaskMayThrow {
void run() throws CustomException;
}
private static boolean runTask(TaskMayThrow task, List<CustomException> errList) {
try {
task.run();
return true;
} catch(CustomException e) {
errList.add(e);
return false;
}
}
List<CustomException> exceptionList = new ArrayList<CustomException>();
public void editProduct(String articleNumber, ...)
{
runTask(() -> product.setArticleNumber(articleNumber), exceptionList);
runTask(() -> product.setDescription(description), exceptionList);
// The same for all other setters
// Do whatever you want with the error list
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.