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);
      }
    }
  • 相关阅读:
    centos 7 安装nvidia显卡驱动
    Ubuntu 16.04LTS 安装 MATLAB 2014B
    Linux 查看CPU温度
    pip: unsupported locale setting
    ubuntu 卸载从源码安装的 emacs
    html css使用特殊自定义字体避免侵权
    JS操作iframe父级子级元素,jquery自动点击iframe里按钮
    Iframe标签显示目标网页的指定区域,视频可全屏可缩小
    禁止所有搜索爬虫访问网站指定目录robots.txt
    ThinkPHP5.0、5.1和6.0教程文档合集(免费下载)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/13261439.html
Copyright © 2011-2022 走看看