ValidationException extends RuntimeException { @Override public String getMessage() { StringBuilder sb = new StringBuilder(); String msg = super.getMessage(); if(msg != null) { sb.append(msg).append('\n'); } if(causes != null) { for(Throwable t : causes) { if(t != null) { msg = t.getMessage(); sb.append((msg == null) ? t.getClass().getName() : msg).append('\n'); } } } int len = sb.length(); return (len == 0) ? getClass().getName() : sb.deleteCharAt(len - 1).toString(); } ValidationException(Throwable cause);  ValidationException(String message, Throwable cause);  ValidationException(String message, Throwable[] causes);  ValidationException(String message, List<? extends Throwable> causes);  ValidationException(String message);  ValidationException(Throwable[] causes);  ValidationException(List<? extends Throwable> causes); Throwable[] getCauses(); @Override String getMessage();  }
@Test public void testMessages() { String s; String message = "exception message 12345"; Throwable[] causes; causes = new Throwable[] {new RuntimeException("12345"), new RuntimeException("abcd") }; s = new ValidationException(message, causes).getMessage(); assertTrue(s.startsWith(message)); assertTrue(s.contains("\n12345")); assertTrue(s.contains("\nabcd")); s = new ValidationException(causes).getMessage(); assertTrue(s.startsWith("12345\n")); assertTrue(s.endsWith("\nabcd")); causes = new Throwable[] { new RuntimeException(), new RuntimeException() }; s = new ValidationException(message, causes).getMessage(); assertTrue(s.startsWith(message)); assertTrue(s.indexOf("java.lang.RuntimeException") != s.lastIndexOf("java.lang.RuntimeException")); s = new ValidationException(causes).getMessage(); assertEquals("java.lang.RuntimeException\njava.lang.RuntimeException", s); causes = new Throwable[] { new RuntimeException(), new RuntimeException("67890") }; s = new ValidationException(message, causes).getMessage(); assertTrue(s.startsWith(message)); assertTrue(s.contains("\njava.lang.RuntimeException")); assertTrue(s.contains("\n67890")); s = new ValidationException(causes).getMessage(); assertTrue(s.startsWith("java.lang.RuntimeException\n")); assertTrue(s.endsWith("\n67890")); causes = new Throwable[] {}; s = new ValidationException(message, causes).getMessage(); assertEquals(s, message); s = new ValidationException(causes).getMessage(); assertEquals(s, ValidationException.class.getName()); causes = null; s = new ValidationException(message, causes).getMessage(); assertEquals(s, message); s = new ValidationException(causes).getMessage(); assertEquals(s, ValidationException.class.getName()); Throwable cause = null; s = new ValidationException(message, cause).getMessage(); assertEquals(s, message); s = new ValidationException(cause).getMessage(); assertEquals(s, ValidationException.class.getName()); }