Finding specific type of custom exception on all inner exceptions

granadaCoder :

I am trying to recursively find all inner exceptions (getCause's) from a top level exception...of a specific instance type.

public class MyCustomRunTimeException extends RuntimeException {

    public MyCustomRunTimeException() {
    }

    public MyCustomRunTimeException(Exception innerException) {
        super(innerException);
    }
}

Here is what I've tried:

and my early "find" method:

private void findAllSpecificTypeOfInnerExceptions(Exception ex)
{

            Collection<MyCustomRunTimeException> MyCustomRunTimeExceptions = Stream.iterate(ex, Throwable::getCause)
                    .filter(element ->
                            element != null
                                    && element instanceof MyCustomRunTimeException
                    )
                    .map(obj -> (MyCustomRunTimeException) obj)
                    .collect(Collectors.toList());

}

It is not working. :( I've tried several other things (not shown yet)... I'll post them as "appends" to this question if I get anything that doesn't throw an exception. Not working.... I'm getting NullPointers exceptions. and (depending on my tweaks) java.lang.reflect.InvocationTargetException exception.

Here is some examples that would find 1:N "matches".

    Exception exampleOne = new MyCustomRunTimeException();

    Exception exampleTwo = new Exception(new MyCustomRunTimeException());

    Exception exampleThree =new Exception(new Exception(new MyCustomRunTimeException()));

    Exception exampleFour =new Exception(new Exception(new MyCustomRunTimeException(new ArithmeticException())));
Michael Berry :

This is a case where I really think it's easiest doing the recursive bit in a loop, rather than using streams entirely:

List<Throwable> exlist = new ArrayList<>();
while(ex.getCause()!=null) {
    exlist.add(ex.getCause());
    ex = ex.getCause();
}
exlist = exlist.stream().filter(e -> e instanceof MyCustomRunTimeException).collect(Collectors.toList());

There are better options in Java 9+, and if you use external libraries, but not really in core Java 8 (hacks do exist, but that makes it much less clear than the above, IMHO, and I really don't see that it's worth introducing an external library just for this purpose.)

Or this variation:

private <T extends Throwable> Collection<T> aggregateSubClassExceptions(Throwable inputEx, Class<T> type) {

    Collection<T> returnItems = new ArrayList<>();
    Throwable exc = inputEx;
    while (exc != null) {
        if (type.isInstance(exc)) {
            returnItems.add(type.cast(exc));
        }
        exc = exc.getCause();
    }

    return returnItems;
}

This code was getting the specific type AND any subclasses:

    Collection<MyCustomRunTimeException> testItOutCollection;



    Exception exampleOne = new MyCustomRunTimeException();
    Exception exampleTwo = new Exception(new MyCustomRunTimeException());
    Exception exampleThree = new Exception(new Exception(new MyCustomRunTimeException()));
    Exception exampleFour = new Exception(new Exception(new MyCustomRunTimeException(new ArithmeticException())));
    MyCustomRunTimeException exampleFive = new MyCustomRunTimeException(new MySubMyCustomRunTimeException(new MyCustomRunTimeException(new ArithmeticException())));

    testItOutCollection = this.aggregateSubClassExceptions(exampleOne, MyCustomRunTimeException.class);
    testItOutCollection = this.aggregateSubClassExceptions(exampleTwo, MyCustomRunTimeException.class);
    testItOutCollection = this.aggregateSubClassExceptions(exampleThree, MyCustomRunTimeException.class);
    testItOutCollection = this.aggregateSubClassExceptions(exampleFour, MyCustomRunTimeException.class);
    testItOutCollection = this.aggregateSubClassExceptions(exampleFive, MyCustomRunTimeException.class);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=124917&siteId=1
Recommended