自定义异常类RuntimeException:
import lombok.Getter; @Getter public class LyException extends RuntimeException { /** * 异常状态码信息 */ private int status; public LyException(int status) { this.status = status; } public LyException(int status, String message) { super(message); this.status = status; } public LyException(int status, String message, Throwable cause) { super(message, cause); this.status = status; } //Throwable为Exception顶级父类 public LyException(int status, Throwable cause) { super(cause); this.status = status; } }
使用的时候:
throw new LyException(500, "SpuDetail新增失败");