SpringBoot:自定义异常

我们在实现自定义异常的时候,我们需要继承 RuntimeException ,参考代码:

java 复制代码
/**
 * <b>Function: </b> todo
 *
 * @program: BizException
 * @Package: com.kingbal.king.common.core.exception
 * @author: dingcho
 * @date: 2024/09/14
 * @version: 1.0
 * @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
 */
public class BizException extends RuntimeException{

	private static final long serialVersionUID = 9104770577278712502L;

	/**
	 * 状态码
	 */
	private int code;

	private ErrorCodeEnum errorEnum;

	public BizException(ErrorCodeEnum errorEnum, Throwable cause) {
		super(errorEnum.getMsg(), cause);
		this.code = errorEnum.getCode();
		this.errorEnum = errorEnum;
	}

	public BizException(ErrorCodeEnum errorEnum) {
		super(errorEnum.getMsg());
		this.code = errorEnum.getCode();
		this.errorEnum = errorEnum;
	}

	public BizException(ErrorCodeEnum errorEnum, String message) {
		super(message);
		this.code = errorEnum.getCode();
		this.errorEnum = errorEnum;
	}

}
java 复制代码
/**
 * <b>Function: </b> todo
 *
 * @program: ErrorCodeEnum
 * @Package: com.kingbal.king.common.core.constant.enums
 * @author: songjianlin
 * @date: 2024/09/14
 * @version: 1.0
 * @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
 */
@Getter
@AllArgsConstructor
public enum ErrorCodeEnum {

	BIZ_PARAM_ERROR(400,"非法的参数!"),

	;


	/**
	 * 类型
	 */
	private final Integer code;

	/**
	 * 描述
	 */
	private final String msg;

}

使用:

java 复制代码
// 调用

throw new BizException(ErrorCodeEnum.BIZ_PARAM_EROOR);
相关推荐
华子w9089258599 天前
基于 SpringBoot+JSP 的医疗预约与诊断系统设计与实现
java·spring boot·后端
feifeigo1239 天前
Java 正则表达式高级用法
java·mysql·正则表达式
empti_9 天前
Java中的List实现类详解
java
亲爱的非洲野猪9 天前
一次性理解Java垃圾回收--简单直接方便面试时使用
java·jvm·面试
小阳拱白菜9 天前
Maven生命周期,测试
java·开发语言
皮皮林5519 天前
Java实现简易即时通讯系统
java
java—大象9 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
找不到、了9 天前
kafka消费的模式及消息积压处理方案
java·kafka
Mutig_s9 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis