ExceptionUtils
package com.test; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.security.web.util.ThrowableAnalyzer; public class ErrorTest { private static ThrowableAnalyzer throwableAnalyzer = new ThrowableAnalyzer(); public static void main(String[] args) { try { try { try { int i = 2; int j = i / 0; } catch (ArithmeticException e) { throw new TestRuntimeException("An error occurs inner", e); } } catch (TestRuntimeException e) { throw new TempestRuntimeException("An error occurs outer", e); } } catch (Exception e) { System.out.println(e.getMessage());//An error occurs outer System.out.println(e.getCause());//com.test.TestRuntimeException: An error occurs inner /*Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e); TempestRuntimeException ex = (TempestRuntimeException) throwableAnalyzer .getFirstThrowableOfType( TempestRuntimeException.class, causeChain);*/ System.out.println(ExceptionUtils.getMessage(e));//TestRuntimeException: An error occurs outer System.out.println(ExceptionUtils.getStackTrace(e)); System.out.println(ExceptionUtils.getRootCause(e));//java.lang.ArithmeticException: / by zero System.out.println(ExceptionUtils.getRootCauseMessage(e));//ArithmeticException: / by zero Throwable[] causeChain = ExceptionUtils.getThrowables(e); } } }
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
package com.test; public class TestRuntimeException extends RuntimeException { public TempestRuntimeException(Throwable e) { super(e); } public TestRuntimeException(String string) { super(string); } public TestRuntimeException(String string, Throwable e) { super(string, e); } }