KeyTokenInvalidException extends RuntimeException { public static boolean isKeyTokenInvalidException(Throwable t) { while (t != null) { if (t instanceof KeyTokenInvalidException) { return true; } t = t.getCause(); } return false; } KeyTokenInvalidException(String key); static boolean isKeyTokenInvalidException(Throwable t);  }
@Test public void testIsKeyTokenInvalidException() throws Exception { KeyTokenInvalidException exception = new KeyTokenInvalidException("test"); RuntimeException keyTokenCauseException = new RuntimeException("key token cause", exception); assertTrue(KeyTokenInvalidException.isKeyTokenInvalidException(exception)); assertTrue(KeyTokenInvalidException.isKeyTokenInvalidException(keyTokenCauseException)); assertFalse( KeyTokenInvalidException.isKeyTokenInvalidException(new RuntimeException("non key token"))); }