Exceptions { public static Throwable rootCause(Throwable ex) { Throwable cause = ex.getCause(); if (cause != null) { return rootCause(cause); } return ex; } private  Exceptions(); static Throwable rootCause(Throwable ex); static T anyCauseOf(Throwable ex, Class<T> type); static Throwable firstCauseNotOf(Throwable ex, Class<?>... types);  }
@Test public void testRootException() { A top=new A("a",new B("b",new C("c",new D("root")))); Assert.assertEquals("type", D.class,Exceptions.rootCause(top).getClass()); Assert.assertEquals("message", "root",Exceptions.rootCause(top).getMessage()); }