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);
相关推荐
luming-024 分钟前
java报错解决:sun.net.utils不存
java·经验分享·bug·.net·intellij-idea
北海有初拥12 分钟前
Python基础语法万字详解
java·开发语言·python
alonewolf_9915 分钟前
Spring IOC容器扩展点全景:深入探索与实践演练
java·后端·spring
super_lzb18 分钟前
springboot打war包时将外部配置文件打入到war包内
java·spring boot·后端·maven
毛小茛19 分钟前
芋道管理系统学习——项目结构
java·学习
东北小狐狸-Hellxz36 分钟前
解决java客户端连接ssh失败问题
java·网络·ssh
悟能不能悟36 分钟前
HttpServletRequest request获取整个headers有什么方法
java
__万波__37 分钟前
二十三种设计模式(二十)--解释器模式
java·设计模式·解释器模式
网安_秋刀鱼1 小时前
【java安全】反序列化 - CC1链
java·c语言·安全
零度@1 小时前
Java消息中间件-Kafka全解(2026精简版)
java·kafka·c#·linq