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