ExceptionUtils

ExceptionUtils工具类的使用

描述

  • 该类属于org.apache.commons.lang3.exception.ExceptionUtils

  • 通过传入的Throwable对象,进而获取详细的错误信息。包括:

    • 错误的原因
    • 错误的堆栈信息
    • 报错的根原因
    • 错误的根消息
    • 错误的数量
    • 等等
  • 通过ExceptionUtils,我们可以拿到程序报错的详细信息,同步到日志库当中,不用我们手动区编写代码抽取日志报错信息

方法

  • getMessage(throwable);获取错误信息
  • getStackTrace(throwable);获取错误堆栈信息
  • getRootCauseMessage(throwable);获取错误的根消息
  • getRootCause(throwable);获取错误的根原因
  • getStackFrames(throwable);获取栈帧消息
  • getThrowableCount(throwable);获取报错数
  • getThrowables(throwable);获取throwable数

测试demo

controller层

package com.hfwas.main.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ExceptionTestController
 * @Description
 * @Author <a href="hfwas1024@gmail.com">HFwas</a>
 * @Date: 3:16 下午
 * @Version: 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/exception")
public class ExceptionTestController {

    @GetMapping("/aa")
    public void test01() {

        int a = 1/ 0;

        System.out.println("test1");
    }
}

handle层

package com.hfwas.main.exception;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ExceptionTests
 * @Description
 * @Author <a href="hfwas1024@gmail.com">HFwas</a>
 * @Date: 3:09 下午
 * @Version: 1.0
 **/
@Slf4j
@RestControllerAdvice
public class ExceptionHanler {

    @ExceptionHandler(value = Exception.class)
    public void handler(HttpServletRequest req, Throwable throwable){
				// // ArithmeticException: / by zero
        String message = ExceptionUtils.getMessage(throwable);
				
        String stackTrace = ExceptionUtils.getStackTrace(throwable);
        // ArithmeticException: / by zero
        String rootCauseMessage = ExceptionUtils.getRootCauseMessage(throwable);
        // java.lang.ArithmeticException: / by zero
        Throwable rootCause = ExceptionUtils.getRootCause(throwable);
        // ["java.lang.Arith...", "	at com.hfwas.m...", "	at sun.reflect...", "	at sun.reflect...", "	at sun.reflect...", +47 more]
        String[] stackFrames = ExceptionUtils.getStackFrames(throwable);
        // 1
        int throwableCount = ExceptionUtils.getThrowableCount(throwable);
       
        Throwable[] throwables = ExceptionUtils.getThrowables(throwable);

        log.info("错误日志开始打印");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值