ExceptionUtil { public static <T> T findCause(Throwable throwable, Class<T> needle) { Throwable target = throwable; while (target != null) { if (needle.isInstance(target)) { return needle.cast(target); } target = target.getCause(); } return null; } private  ExceptionUtil(); static T findCause(Throwable throwable, Class<T> needle);  }
@Test public void shouldFindTheCause() { assertNotNull( ExceptionUtil.findCause(new RuntimeException(new RuntimeException(new IOException())), IOException.class)); }
@Test public void shouldNotFindTheCause() { assertNull( ExceptionUtil.findCause(new RuntimeException(new IOException()), UnsupportedOperationException.class)); }