zoukankan      html  css  js  c++  java
  • 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);
      }
    }
  • 相关阅读:
    SQL注入常见处理方式
    git操作常用
    crontab 基本参数
    替代PHP格式化成无符号数后精度不对问题
    替代PHP两个大数相乘会有精度损失
    排序算法
    迁移服务器资源到新服务器
    数据库分库分表思路
    drupal 常用表单元素
    drupal模块开发
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/13261439.html
Copyright © 2011-2022 走看看