[JAVA基礎篇23]—JAVA異常工具類ExceptionUtils的使用

項目中一般是這樣處理異常的:

import com.tellme.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

@Slf4j
public class TestExceptionUtils {
    //處理異常的方式
    private static void say() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            //將e傳入,即保持了異常鏈的信息。否則的話,異常鏈便斷了。
            throw new RuntimeException("xx", e);
        }
    }
}

org.apache.commons.lang3.exception.ExceptionUtils工具類的用法:

import com.tellme.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

@Slf4j
public class TestExceptionUtils {

    public static void main(String[] args) {
        try {
            say();
        } catch (Exception e) {
            log.error("", e);
            System.out.println("----------");
            //RuntimeException:xx  (打印的是直接異常)
            System.out.println("ExceptionUtils.getMessage(e):" + ExceptionUtils.getMessage(e));
            //ArithmeticException: / by zero   (打印的是根異常,但是若是拋出異常時,沒將跟異常向外拋出,那麼捕獲到的依舊是RuntimeException:xx)
            System.out.println("ExceptionUtils.getRootCauseMessage(e):" + ExceptionUtils.getRootCauseMessage(e));
            //打印的是異常堆棧信息
            System.out.println("ExceptionUtils.getStackTrace(e):" + ExceptionUtils.getStackTrace(e));
            //java.lang.ArithmeticException: / by zero   (打印的是根異常,但是若是拋出異常時,沒將跟異常向外拋出,那麼返回null)
            System.out.println("ExceptionUtils.getRootCause(e):" + ExceptionUtils.getRootCause(e));
            //異常鏈中異常的數量:2
            System.out.println("ExceptionUtils.getThrowableCount(e):" + ExceptionUtils.getThrowableCount(e));
            //java.lang.ArithmeticException: / by zero  數組,第一位的的是根異常信息
            System.out.println("ExceptionUtils.getRootCauseStackTrace(e)[0]:" + ExceptionUtils.getRootCauseStackTrace(e)[0]);
            //java.lang.RuntimeException: xx  數組,第一位的是直接異常信息
            System.out.println("ExceptionUtils.getStackFrames(e)[0]:" + ExceptionUtils.getStackFrames(e)[0]);
            //異常鏈的數組:[java.lang.RuntimeException: xx, java.lang.ArithmeticException: / by zero]
            System.out.println("ExceptionUtils.getThrowableList(e):" + ExceptionUtils.getThrowableList(e));
            //自定義的異常鏈信息(打印直接異常的詳細信息):ExceptionUtil.getLogErrorMessage(e):xx||RuntimeException||com.tellme.test.TestExceptionUtils.say(TestExceptionUtils.java:38)
            System.out.println("ExceptionUtil.getLogErrorMessage(e):" + ExceptionUtil.getLogErrorMessage(e));
        }
    }

    private static void say() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            //將e傳入,即保持了異常鏈的信息。否則的話,異常鏈便斷了。
            throw new RuntimeException("xx", e);
        }
    }
}

自定義的異常類:

    /**
     * 獲取到異常描述
     *
     * @param e 異常信息
     * @return "異常信息||異常類名||異常位置"
     */
    public static String getLogErrorMessage(Exception e) {
        return e.getMessage() + "||" + e.getClass().getSimpleName() + "||" + e.getStackTrace()[0];
    }

可以將異常信息入庫,這樣便可以快速定位到問題原因。

通用的異常類

自定義異常可以這樣設置,將異常對象傳遞下來


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BusinessException extends RuntimeException {

    private static final Logger logger = LoggerFactory.getLogger(BusinessException.class);


    private static final long serialVersionUID = 1L;

    private String code = "";

    private String msg = "";

    //使用super(msg, cause)將異常對象組裝成鏈存儲起來
    public BusinessException(String code, String msg, Throwable cause) {
        super(msg, cause);
        this.setCode(code);
        this.setMsg(msg);
    }

    /**
     *
     */
    public BusinessException() {
        super();
    }
    public BusinessException(String message) {
        super(message);
    }

    /**
     *
     * @param message
     *          消息
     * @param cause
     *          原因
     */
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.setMsg(msg);
    }

    /**
     *
     * @param code
     * @param msg
     */
    public BusinessException(String code, String msg) {
        super(msg);
        this.setCode(code);
        this.setMsg(msg);
    }


    /**
     *
     * @param cause
     *          原因
     */
    public BusinessException(Throwable cause) {
        super(cause);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章