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);
相关推荐
代码不停15 分钟前
JavaEE初级——Thread多线程
java·jvm·java-ee
xxxxxxllllllshi24 分钟前
Cookie、Session、JWT、SSO,网站与 APP 登录持久化与缓存
java·开发语言·jvm·数据结构·缓存·面试
郑重其事,鹏程万里39 分钟前
commons-io
java
Moshow郑锴1 小时前
CSP 配置指南:SpringBoot/Express 实操 + 多域名适配,防 XSS 攻击超简单
spring boot·express·xss
爱吃烤鸡翅的酸菜鱼1 小时前
从零掌握贪心算法Java版:LeetCode 10题实战解析(上)
java·算法
计算机徐师兄1 小时前
Java基于SpringBoot的农场管理系统小程序【附源码、文档说明】
java·微信小程序·小程序·农场管理系统小程序·java农场管理系统小程序·java农场管理系统微信小程序·农场管理微信小程序
草字2 小时前
uniapp 打开横竖屏。usb调试时可以横竖屏切换,但是打包发布后却不行?
java·前端·uni-app
Cg136269159742 小时前
多态的定义
java·开发语言
云霄IT2 小时前
新版电脑微信4.1.x.x小程序逆向之——寻找小程序存放位置目录和__APP__.wxapkg
java·微信·小程序
微信api接口介绍2 小时前
微信社群管理开发
java·开发语言·网络·微信