Reputation: 2495
How can i process A Multi exception Exception on a file with multiple issues to be reported.
I have a case of multi processing step where different exceptions can occur (e.g they will be made asynchronous later). I am using (might be anti-pattern for fail fast) list of exceptions then once they are completed and check for exceptions
I have my own customized exceptions category (For each asynch tasks) (extends Exception
class) but implements an interface to have additional information like a key value pair specific to the message
Sample Implementation
List<Files> Folder;
//Now processed into below folders
List<Files> EnglishFolder;
List<Files> KoreanFolder;
List<Files> SpanishFolder;
//Now each task accumulates own exceptions.
EnglishException extends Exception implements InfoMapping
{
private EnumMap<CUSTOMENUM,STRING> info;
EnglishException(String message){super(message);}
EnglishException(String message, Exception why){super(message);}
public void addInfo(CUSTOMENUM key,String value){info.add(key,value}
}
My problem is if I know what issues i create these exception objects in each tasks but i don't throw them. But if there are general exceptions i just catch them and add it as
List<EnglishException> englishErrors;
//blah blah
englishErrors.add(new EnglishException("I found this error"));
//if generic exception in catch
englishErrors.add(new EnglishException("Unknown English error",e));
//returns without throwing exception
Now i need to synch up all tasks
neatly package all exceptions into an XML file
so i need to support 1 exception class that supports a list of Exceptions class
has only this Exception(String message, Throwable cause)
only supports single inner exception.
2 questions:
Being new to streams, can i make the above logic more readable/simple like (am very naive to java 8, just reading so forgive me if it doesnt make any sense). Not looking to get answers, may be just pointers to what in streams to look for to achieve this.
Streams.Of(englishExceptionList,spanishExceptionList)
.reduce(parentException)
.ifAny(throw parentException)
Upvotes: 0
Views: 426
Reputation: 298153
You can use Throwable.addSuppressed(Throwable)
which allows an arbitrary number of throwables to be recorded (and later retrieved via getSuppressed()
) and is also a better fit semantically than recording the other throwables as “cause”.
Since this is a kind of modification of one of the throwables, reduce
is not the correct operation for that (and it would not provide the intended result in a parallel operation).
This doesn’t look like an operation that needs an optimization for a high number of elements, hence, the best solution is to straight-forwardly collect all elements into a List
and use
if(!listOfAllExceptions.isEmpty()) {
Throwable t = listOfAllExceptions.get(0);
listOfAllExceptions.subList(1, listOfAllExceptions.size()).forEach(t::addSuppressed);
throw t;
}
The type of t
needs to be adjusted to an unchecked or declared exception.
Upvotes: 1