java elegant custom exception handling

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wsgsm/article/details/99991782
Use custom exception

In java project, the general use custom exception to the program to do some special treatment. Defined manner from abnormal use, to more clearly show the logical problems program. How elegant use exceptions, is a science in itself.

In this paper, to handle enumeration custom exception generated using lombok get / set, and a construction method

  1. New abnormal enumeration class
import lombok.AllArgsConstructor;

/**
 * @ClassName: ExceptionEnum
 * @Description: TODO(异常枚举)
 * @Author: flyingkid
 * @Date: 2019-08-16 20:08:42
 */
@AllArgsConstructor
public enum ExceptionEnum {

    UNKNOWN_EXCEPTION("unknown_exception","未知异常"),

    SYSTEM_EXCEPTION("system_exception","系统异常"),

    PARAMETER_EXCEPTION("parameter_exception","参数异常");


    public final String code;

    public final String message;

}
  1. Custom exception class, inherited RuntimeException
import com.example.common.enums.ExceptionEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: ExampleException
 * @Description: TODO(自定义异常)
 * @Author: flyingkid
 * @Date: 2019-08-16 20:07:25
 */
@Data
@Slf4j
public class ExampleException extends RuntimeException{

    private static final long serialVersionUID = -6979901566637669960L;

    private ExceptionEnum exceptionEnum;

    private String code;

    private String message;


    public ExampleException(String code,String message){
        super(message);
        this.code = code;
        this.message = message;
    }

    public ExampleException(ExceptionEnum exceptionEnum){
        super(exceptionEnum.message);
        this.exceptionEnum = exceptionEnum;
    }


    public ExampleException(String code,String message,Throwable throwable){
        super(message,throwable);
        this.code = code;
        this.message = message;
    }

    public ExampleException(ExceptionEnum exceptionEnum,Throwable throwable){
        super(exceptionEnum.message,throwable);
        this.exceptionEnum = exceptionEnum;
    }


    /**
     * @description TODO 重写Throwable中printStackTrace方法,打印异常信息
     * @return void
     * @date 2019/8/21 下午7:57
     * @author flyingkid
     */
    @Override
    public void printStackTrace(){
        if (exceptionEnum != null){
            log.info("异常代码: {}, 异常信息: {}",exceptionEnum.code,exceptionEnum.message);
            return;
        }
        log.info("异常代码: {}, 异常信息: {}",code,message);
    }

}
  1. use
 public static void main(String[] args) {
        try {
            if (true){
                //抛出异常
                throw new ExampleException(ExceptionEnum.SYSTEM_EXCEPTION);
            }
        }catch (ExampleException e){
            //异常捕获
            e.printStackTrace();
        }
    }

Console Print

19:36:10.678 [main] INFO com.example.common.exception.ExampleException - 异常代码: system_exception, 异常信息: 系统异常

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/wsgsm/article/details/99991782