java優雅的處理程式中的異常

unascribed發表於2020-09-23

java優雅的處理程式中的異常

一、自定義異常類

(1)繼承 Exception 或 RuntimeException
(2)定義構造方法

/**
 * @author lqh
 * @date 2020/9/22
 * 業務邏輯異常
 */
public class ServiceException extends RuntimeException{

    private String code;
    private String msg;

    public ServiceException() {
    }

    public ServiceException(String msg) {
        this.msg = msg;
    }

    public ServiceException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    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;
    }
}

二、@ControllerAdvice

  @ControllerAdvice 實現全域性異常處理,需配合@ExceptionHandler(註解用來指明異常的處理型別)使用。

/**
 * @author lqh
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ServerResponse exceptionHandler(Exception e) {
        return ServerResponse.error(ResponseCode.SERVER_ERROR.getMsg());
    }
    @ResponseBody
    @ExceptionHandler(ServiceException.class)
    public ServerResponse serviceExceptionHandler(ServiceException se) {
        return ServerResponse.error(se.getMsg());
    }
}

  ResponseCode是自定義列舉類,用於返回統一的狀態

/**
 * @author lqh
 * @date 2020/9/21
 * 響應狀態碼
 */
public enum ResponseCode {

    // 系統模組
    SUCCESS(200, "操作成功"),
    SAVE_SUCCESS(201,"儲存成功"),
    DELETE_SUCCESS(202,"刪除成功!"),
    UPDATE_SUCCESS(403,"更新成功!"),

    ERROR(400, "操作失敗"),
    SAVE_ERROR(401,"儲存失敗"),
    DELETE_ERROR(402,"刪除失敗!"),
    UPDATE_ERROR(403,"更新成功"),

    SERVER_ERROR(500, "伺服器異常"),
    EXCEPTION(-1,"Exception"),

    // 使用者模組 0xxxx
    NEED_LOGIN(1001, "登入失效"),
    USERNAME_OR_PASSWORD_EMPTY(1002, "使用者名稱或密碼不能為空"),
    USERNAME_OR_PASSWORD_WRONG(1003, "使用者名稱或密碼錯誤"),
    USER_NOT_EXISTS(1004, "使用者不存在"),
    WRONG_PASSWORD(1005, "密碼錯誤"),
}

四、異常e相關方法(解釋在方法註釋中)

 @Test
    public void test01(){
        try {
            System.out.println(1/0);
        }catch (Exception e){
            /**
             * 獲取異常種類和錯誤資訊
             * java.lang.ArithmeticException: / by zero
             */
            System.out.println(e.toString());

            /**
             *獲取錯誤資訊
             * / by zero
             */
            System.out.println(e.getMessage());
            /**
             * 獲取異常類的Class
             *class java.lang.ArithmeticException
             */
            System.out.println(e.getClass());
            /**
             *獲取異常類名稱
             *java.lang.ArithmeticException
             */
            System.out.println(e.getClass().getName());
            /**
             * 會打出詳細異常,異常名bai稱,出錯位置,便於除錯用
             * java.lang.ArithmeticException: / by zero
             at com.bluewit.exception.ExceptionTest.test01(ExceptionTest.java:13)
             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
             at java.lang.reflect.Method.invoke(Method.java:498)
             at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
             at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
             at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
             at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
             at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
             at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
             at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
             at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
             at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
             at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
             at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
             at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
             at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
             at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
             at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
             at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
             at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
             at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
             */
            e.printStackTrace();

            /**
             * 返回的是通過getOurStackTrace方法獲取的StackTraceElement[]陣列,而這個StackTraceElement是ERROR的每一個cause by的資訊。
             * [Ljava.lang.StackTraceElement;@4c203ea1
             */
            System.out.println(e.getStackTrace());

            /**
             * 返回一個包含所有被抑制的異常,通常由一個陣列try -with-resources語句,為了實現這一例外。 如果沒有例外被抑制或抑制被禁止 ,則返回一個空陣列
             */
            System.out.println(e.getSuppressed());

            /**
             *回此異常的原因(嘗試載入類時發生錯誤引發的異常;否則返回 null)
             * null
             */
            System.out.println(e.getCause());
        }
    }

五、自定義異常使用

(1)舉例一:程式中根據業務判斷丟擲異常資訊

	if(1=1){
		throw new ServiceException(ResponseCode.SUCCESS.getMsg());
	}

(2)舉例二:根據異常資訊丟擲具體業務資訊

 	  try {
            System.out.println(1/0);
        }catch (Exception e){
        	if(e.toString().contains("SyntaxError")){
                throw new ServiceException(ResponseCode.GRMMAR_RULES_ILLEGAL.getMsg());
            }
	   } 

相關文章