当前位置: 首页 > news >正文

Java的全局异常处理代码

第一步:先写一个异常管理类:

package com.example.firefighting.exceptions;import com.example.firefighting.utils.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;import static com.example.firefighting.utils.XiaofangConstants.SYSTEM_ANOMALY;/*** 异常处理* @author IT空门_门主* @date 2024/7/2*/@RestControllerAdvice
public class XiaofangException {/*** 全局异常*/@ExceptionHandler(value = Exception.class)public Result globalException() {return Result.fail(SYSTEM_ANOMALY);}/*** 业务异常处理* @param e* @return*/@ExceptionHandler(value = ServiceException.class)public Result serviceException(ServiceException e) {return Result.fail(e.getCode(), e.getMessage());}}
  1. 我这里定义了两个异常:一个是全局异常和业务的异常。 项目大的情况下可以自定义更多的异常

第二步:业务的异常继承了运行异常

package com.example.firefighting.exceptions;import lombok.Data;
import lombok.EqualsAndHashCode;/*** 业务逻辑异常 Exception*/
@Data
@EqualsAndHashCode(callSuper = true)
public final class ServiceException extends RuntimeException {/*** 业务错误码*/private Integer code;/*** 错误提示*/private String message;public ServiceException(Integer code, String message) {this.code = code;this.message = message;}}
  1. 业务的异常中,可以通过自己的业务,编写自己的业务逻辑。
  2. 我是为了统一管理业务错误,编写了有参构造接收错误信息

第三步:工具类

package com.example.firefighting.exceptions.utils;import com.example.firefighting.exceptions.ServiceException;
import com.example.firefighting.utils.ErrorCode;import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;/*** @author IT空门_门主* @description: 自定义异常工具类* @date 2024/7/2*/public class ServiceExceptionUtil {/*** 错误码提示模板*/private static final ConcurrentMap<Integer, String> MESSAGES = new ConcurrentHashMap<>();/*** 自定义异常*/public static ServiceException exception(ErrorCode errorCode) {String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMsg());return exception0(errorCode.getCode(), messagePattern);}private static ServiceException exception0(Integer code, String messagePattern) {return new ServiceException(code, messagePattern);}}

1.ConcurrentHashMap:

用于多线程环境,允许多个线程同时读写映射而不会引起数据不一致的问题。ConcurrentHashMap是ConcurrentMap的一个实现,提供了高效的并发性能。

第四步:错误枚举

package com.example.firefighting.enums;import com.example.firefighting.utils.ErrorCode;/*** 错误码枚举类* device 系统 从 1001开始-9999结束 不可重复* @author IT空门_门主* @date 2024/7/2*/public interface ErrorCodeConstants {ErrorCode USER_DOES_NOT_EXIST = new ErrorCode(1001, "该用户不存在");ErrorCode USER_IS_DISABLED = new ErrorCode(1002, "该用户已被禁用");ErrorCode INCORRECT_USERNAME_OR_PASSWORD = new ErrorCode(1003, "用户名或密码错误");
}

1.管理业务所有的错误码

商务合作:z13135361785  

技术交流:z13135361785  

http://www.lryc.cn/news/389363.html

相关文章:

  • Hi3861 OpenHarmony嵌入式应用入门--LiteOS semaphore作为锁
  • 注意!年龄越大,社交圈子越窄?其实这是老人的理性选择!数学家告诉你:何时该跳槽,何时该坚守!你必须知道的三个智慧:让你的人生更加精彩!
  • [SwiftUI 开发] 嵌套的ObservedObject中的更改不会更新UI
  • 全面了解机器学习
  • 作为图形渲染API,OpenGL和Direct3D的全方位对比。
  • 安装Rabbitmq遇到的坑
  • React+TS 从零开始教程(4):useEffect
  • 网络安全学习路线图(2024版详解)
  • 你了解人工智能吗?
  • 如何使用Vue.js实现动态文档生成与下载功能
  • web基础及http协议
  • 【vuejs】vue-router 之 addRoute 动态路由的应用总结
  • LeetCode 30. 串联所有单词的子串
  • python本学期所有代码!
  • 武汉星起航:无锡跨境电商加速“出海”,物流升级助品牌全球布局
  • Python+Pytest+Allure+Yaml+Pymysql+Jenkins+GitLab接口自动化测试框架详解
  • stm32-hal库(5)--usart串口通信三种模式(主从通信)(关于通信失败和串口不断发送数据问题的解决)
  • 一文学会LVS:概念、架构、原理、搭建过程、常用命令及实战案例
  • [Go 微服务] Kratos 使用的简单总结
  • 【unity实战】使用旧输入系统Input Manager 写一个 2D 平台游戏玩家控制器——包括移动、跳跃、滑墙、蹬墙跳
  • 【实战】EasyExcel实现百万级数据导入导出
  • Graalvm配置文件与Feature和Substitute机制介绍
  • Appium adb 获取appActivity
  • 调整分区失败致盘无法访问:深度解析与数据恢复全攻略
  • 试用笔记之-汇通计算机等级考试软件一级Windows
  • Java的NIO体系
  • 自下而上的选股与自上而下的选股
  • Tech Talk:智能电视eMMC存储的五问五答
  • scikit-learn教程
  • CentOS 7 搭建rsyslog日志服务器