【Java】Java抛异常到用户界面公共封装
前言
在Java中处理代码运行异常是常见的技术点之一,我们大部分会使用封装的技巧将异常进行格式化输出,方便反馈给用户界面,也是为了代码复用
看看这行代码是怎么处理异常的
CommonExceptionType.SimpleException.throwEx("用户信息不能为空");
1、首先CommonExceptionType是一个枚举类型
public enum CommonExceptionType implements IExceptionType {
CommonException(),
SimpleException(-1, "%s", CommonException),
主要的实现还是这个接口:IExceptionType
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public interface IExceptionType {
IExceptionType getParent();
Integer getErrorCode();
String getErrorInfoFormat();
static final ExceptionConsumer consumerLocal = new ExceptionConsumer();
default IExceptionType initConsumer(Consumer<Exception> consumer) {
consumerLocal.get().add(consumer);
return this;
}
default IExceptionType initConsumer() {
consumerLocal.get().add(null);
return this;
}
default RuntimeException throwEx(Object... args) {
return throwEx((Exception) null, args);
}
default RuntimeException throwEx(Throwable e, Object... args) {
String exceptionInfo = getErrorInfoFormat();
Integer errorCode = getErrorCode();
if (args != null && args.length > 0) {
exceptionInfo = String.format(getErrorInfoFormat(), args);
}
CommonException ex;
if (e == null) {
ex = new CommonException(exceptionInfo);
} else {
try {
ex = new CommonException(e, exceptionInfo, e.getMessage());
} catch (Exception exc) {
ex = new CommonException(e, "%s", e.getMessage());
}
}
ex.setErrorCode(errorCode);
ex.setExceptionType(this);
throw ex;
}
default <T extends Exception> boolean catchEx(Throwable ex) {
return catchEx(ex,null);
}
default <T extends Exception> boolean catchEx(Throwable ex, Consumer<T> callback) {
if (ex instanceof CommonException) {
IExceptionType ce = ((CommonException) ex).getExceptionType();
if (this.equals(ce) || this.isParent(ce)) {
if (callback != null) {
callback.accept((T) ex);
}
return true;
}
}
if (this == CommonExceptionType.CommonException && ex instanceof sunbox.core.exception.CommonException) {
if (callback != null) {
callback.accept((T) ex);
}
return true;
}
if (this == CommonExceptionType.Finally) {
if (callback != null) {
callback.accept((T) ex);
}
return true;
}
return false;
}
default boolean isParent(sunbox.core.exception.CommonException ex) {
if (ex instanceof CommonException) {
IExceptionType ce = ((CommonException) ex).getExceptionType();
if (this.equals(ce) || this.isParent(ce)) {
return true;
}
}
return false;
}
default boolean isParent(IExceptionType et) {
IExceptionType parent = et;
while (parent != null) {
if (parent.equals(this)) {
return true;
}
parent = parent.getParent();
}
return false;
}
default <R, T extends Exception> R tryCatch(Supplier<R> func, Function<T, R> catchFunc) {
Exception ex = null;
try {
if (func != null) {
return func.get();
}
return null;
} catch (Exception e) {
if (this.catchEx(e, null)) {
if (catchFunc != null) {
return catchFunc.apply((T) e);
}
return null;
}
throw e;
}
}
class CommonException extends sunbox.core.exception.CommonException {
private IExceptionType exceptionType;
protected CommonException() {
super("系统异常");
errorInfo = "系统异常";
}
protected CommonException(String errorInfo) {
super(errorInfo);
this.errorInfo=errorInfo;
}
protected CommonException(String format, Object... args) {
super(String.format(format, args));
errorInfo = String.format(format, args);
}
protected CommonException(Throwable e, String format, Object... args) {
super(e, String.format(format, args));
errorInfo = String.format(format, args);
}
protected CommonException(Exception e) {
super(e);
errorInfo = "系统异常";
}
public IExceptionType getExceptionType() {
return exceptionType;
}
public void setExceptionType(IExceptionType exceptionType) {
this.exceptionType = exceptionType;
setErrorCode(exceptionType.getErrorCode());
}
}
}
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;public interface IExceptionType {IExceptionType getParent();Integer getErrorCode();String getErrorInfoFormat();static final ExceptionConsumer consumerLocal = new ExceptionConsumer();default IExceptionType initConsumer(Consumer<Exception> consumer) {consumerLocal.get().add(consumer);return this;}default IExceptionType initConsumer() {consumerLocal.get().add(null);return this;}default RuntimeException throwEx(Object... args) {return throwEx((Exception) null, args);}default RuntimeException throwEx(Throwable e, Object... args) {String exceptionInfo = getErrorInfoFormat();Integer errorCode = getErrorCode();if (args != null && args.length > 0) {exceptionInfo = String.format(getErrorInfoFormat(), args);}CommonException ex;if (e == null) {ex = new CommonException(exceptionInfo);} else {try {ex = new CommonException(e, exceptionInfo, e.getMessage());} catch (Exception exc) {ex = new CommonException(e, "%s", e.getMessage());}}ex.setErrorCode(errorCode);ex.setExceptionType(this);throw ex;}default <T extends Exception> boolean catchEx(Throwable ex) {return catchEx(ex,null);}default <T extends Exception> boolean catchEx(Throwable ex, Consumer<T> callback) {if (ex instanceof CommonException) {IExceptionType ce = ((CommonException) ex).getExceptionType();if (this.equals(ce) || this.isParent(ce)) {if (callback != null) {callback.accept((T) ex);}return true;}}if (this == CommonExceptionType.CommonException && ex instanceof sunbox.core.exception.CommonException) {if (callback != null) {callback.accept((T) ex);}return true;}if (this == CommonExceptionType.Finally) {if (callback != null) {callback.accept((T) ex);}return true;}return false;}default boolean isParent(sunbox.core.exception.CommonException ex) {if (ex instanceof CommonException) {IExceptionType ce = ((CommonException) ex).getExceptionType();if (this.equals(ce) || this.isParent(ce)) {return true;}}return false;}default boolean isParent(IExceptionType et) {IExceptionType parent = et;while (parent != null) {if (parent.equals(this)) {return true;}parent = parent.getParent();}return false;}default <R, T extends Exception> R tryCatch(Supplier<R> func, Function<T, R> catchFunc) {Exception ex = null;try {if (func != null) {return func.get();}return null;} catch (Exception e) {if (this.catchEx(e, null)) {if (catchFunc != null) {return catchFunc.apply((T) e);}return null;}throw e;}}class CommonException extends sunbox.core.exception.CommonException {private IExceptionType exceptionType;protected CommonException() {super("系统异常");errorInfo = "系统异常";}protected CommonException(String errorInfo) {super(errorInfo);this.errorInfo=errorInfo;}protected CommonException(String format, Object... args) {super(String.format(format, args));errorInfo = String.format(format, args);}protected CommonException(Throwable e, String format, Object... args) {super(e, String.format(format, args));errorInfo = String.format(format, args);}protected CommonException(Exception e) {super(e);errorInfo = "系统异常";}public IExceptionType getExceptionType() {return exceptionType;}public void setExceptionType(IExceptionType exceptionType) {this.exceptionType = exceptionType;setErrorCode(exceptionType.getErrorCode());}}
}
这里我们可以看到interface里面,不再是单纯函数的定义,还有函数的实现。这是要转变。这样使接口的实现多了一份灵活性,但是如果接口里单纯的只定义函数,没有函数的实现的话,可能代码逻辑和结构更加清晰一些,这也是过去我们学习的interface接口。
相关文章:

【Java】Java抛异常到用户界面公共封装
前言 在Java中处理代码运行异常是常见的技术点之一,我们大部分会使用封装的技巧将异常进行格式化输出,方便反馈给用户界面,也是为了代码复用 看看这行代码是怎么处理异常的 CommonExceptionType.SimpleException.throwEx("用户信息不…...

基于Redis实现短信验证码登录
目录 1 基于Session实现短信验证码登录 2 配置登录拦截器 3 配置完拦截器还需将自定义拦截器添加到SpringMVC的拦截器列表中 才能生效 4 Session集群共享问题 5 基于Redis实现短信验证码登录 6 Hash 结构与 String 结构类型的比较 7 Redis替代Session需要考虑的问题 8 …...

步入响应式编程篇(二)之Reactor API
步入响应式编程篇(二)之Reactor API 前言回顾响应式编程Reactor API的使用Stream引入依赖Reactor API的使用流源头的创建 reactor api的背压模式发布者与订阅者使用的线程查看弹珠图查看形成新流的日志 前言 对于响应式编程的基于概念,以及J…...

Oracle SQL: TRANSLATE 和 REGEXP_LIKE 的知识点详细分析
目录 前言1. TRANSLATE2. REGEXP_LIKE3. 实战 前言 🤟 找工作,来万码优才:👉 #小程序://万码优才/r6rqmzDaXpYkJZF 1. TRANSLATE TRANSLATE 用于替换字符串中指定字符集的每个字符,返回替换后的字符串 逐一映射输入字…...

RabbitMQ 在实际应用时要注意的问题
1. 幂等性保障 1.1 幂等性介绍 幂等性是数学和计算机科学中某些运算的性质,它们可以被多次应⽤,⽽不会改变初始应⽤的结果. 应⽤程序的幂等性介绍 在应⽤程序中,幂等性就是指对⼀个系统进⾏重复调⽤(相同参数),不论请求多少次,这些请求对系统的影响都是相同的效果. ⽐如数据库…...

算法日记8:StarryCoding60(单调栈)
一、题目 二、解题思路: 题意为让我们找到每个元素的左边第一个比它小的元素,若不存在则输出-1 2.1法一:暴力(0n2) 首先,我们可以想到最朴素的算法:直接暴力两层for达成目标核心代码如下&…...

大象机器人发布首款穿戴式数据采集器myController S570,助力具身智能数据收集!
myController S570 具有较高的数据采集速度和远程控制能力,大大简化了人形机器人的编程。 myController S570 是一款可移动的轻量级外骨骼,具有 14 个关节、2 个操纵杆和 2 个按钮,它提供高数据采集速度,出色的兼容性,…...

【银河麒麟高级服务器操作系统】业务访问慢网卡丢包现象分析及处理过程
了解更多银河麒麟操作系统全新产品,请点击访问 麒麟软件产品专区:product.kylinos.cn 开发者专区:developer.kylinos.cn 文档中心:document.kylinos.cn 交流论坛:forum.kylinos.cn 服务器环境以及配置 【内核版本…...

C语言之饭店外卖信息管理系统
🌟 嗨,我是LucianaiB! 🌍 总有人间一两风,填我十万八千梦。 🚀 路漫漫其修远兮,吾将上下而求索。 C语言之饭店外卖信息管理系统 目录 设计题目设计目的设计任务描述设计要求输入和输出要求验…...

记一次 .NET某数字化协同管理系统 内存暴涨分析
一:背景 1. 讲故事 高级调试训练营里的一位朋友找到我,说他们跑在linux上的.NET程序出现了内存泄露的情况,上windbg观察发现内存都是IMAGE给吃掉了,那些image都标记了 doublemapper__deleted_ 字样,问我为啥会这样&a…...

部门管理查询部门,nginx反向代理,前端如何访问到后端Tomcat 注解@RequestParam
接口开发 增删改通常是不用返回data数据,返回null 列表查询-结果封装,时间 前后端联调测试 nginx反向代理,前端如何访问到后端Tomcat服务器 删除部门...

JS通过ASCII码值实现随机字符串的生成(可指定长度以及解决首位不出现数值)
在之前写过一篇“JS实现随机生成字符串(可指定长度)”,当时写的过于简单和传统,比较粗放。此次针对此问题,对随机生成字符串的功能进行优化处理,对随机取到的字符都通过程序自动来完成。 在写之前ÿ…...

速通Docker === 快速部署Redis主从集群
目录 镜像仓库介绍 持久化你的数据库 连接到其他容器 创建自定义网络 部署主节点 部署从节点 验证部署 总结 在现代应用架构中,Redis作为一个高性能的内存数据库,被广泛应用于缓存、会话存储、实时分析等多个领域。为了提高Redis的可用性和数据的…...

论文笔记(六十三)Understanding Diffusion Models: A Unified Perspective(一)
Understanding Diffusion Models: A Unified Perspective(一) 文章概括引言:生成模型背景:ELBO、VAE 和分层 VAE证据下界(Evidence Lower Bound)变分自编码器 (Variational Autoencoders&#x…...

stm32使用MDK5.35时遇到*** TOOLS.INI: TOOLCHAIN NOT INSTALLED
mdk5.35出现*** TOOLS.INI: TOOLCHAIN NOT INSTALLED的问题!!!! 以管理员身份重新打开MDK5.35.0.0,用keygen破解密码,但是一直提示我是没有破解成功。 解决办法: target 改成ARM...

在Ubuntu上安装RabbitMQ教程
1、安装erlang 因为rabbitmq是基于erlang开发的,所以要安装rabbitmq,首先需要安装erlang运行环境 apt-get install erlang执行命令查是否安装成功:erl,疯狂 Ctrlc 就能退出命令行 2、安装rabbitmq 1、查看erlang与rabbitmq版本…...

【算法】集合List和队列
阿华代码,不是逆风,就是我疯 你们的点赞收藏是我前进最大的动力!! 希望本文内容能够帮助到你!! 目录 零:集合,队列的用法 一:字母异位词分组 二:二叉树的锯…...

uniapps使用HTML5的io模块拷贝文件目录
最近在集成sqlite到uniapp的过程中,因为要将sqlite数据库预加载,所以需要使用HTML5的plus.io模块。使用过程中遇到了许多问题,比如文件路径总是解析不到等。尤其是应用私有文档目录’_doc’。 根据官方文档: 为了安全管理应用的…...

css‘s hover VS mobile
.animation {animation: 30s move infinite linear;/* &:hover {animation-play-state: paused;*/ }原本写的好好的,测试说:“移动端点击滚动条,跳转到其他页面后,返回当前页面,滚动条不滚动;可以优化位…...

工业制造离不开的BOM
在制造业的浩瀚星空中,物料清单(BOM)犹如“北极星”,牢牢指引着产品从设计蓝图迈向实物诞生的全过程。 BOM的分类 按照设计制造的不同阶段,将BOM划分为设计BOM、工艺BOM、制造BOM三种类型。 设计BOM Engineering BO…...

HTML中的`<!DOCTYPE html>`是什么意思?
诸神缄默不语-个人CSDN博文目录 在学习HTML时,我们经常会看到HTML文档的开头出现<!DOCTYPE html>,它是HTML文件的第一行。很多初学者可能会疑惑,为什么需要这行代码?它到底有什么作用呢?在这篇文章中࿰…...

C语言之斗地主游戏
🌟 嗨,我是LucianaiB! 🌍 总有人间一两风,填我十万八千梦。 🚀 路漫漫其修远兮,吾将上下而求索。 C语言之斗地主游戏 目录 程序概述程序设计 Card类CardGroup类Player类LastCards类Land…...

【玩转全栈】----Django制作部门管理页面
目录 大致效果 BootStrap BootStrap简介 BootStrap配置 BootStrap使用 基本配置 部分代码解释及注意: 用户编辑: 新添数据: 删除数据: 大致效果 我先给个大致效果,基本融合了Django、Bootstrap、css、html等等。 基于D…...

Unreal Engine 5 C++ Advanced Action RPG 十章笔记
第十章 Survival Game Mode 2-Game Mode Test Map 设置游戏规则进行游戏玩法 生成敌人玩家是否死亡敌人死亡是否需要刷出更多 肯定:难度增加否定:玩家胜利 流程 新的游戏模式类游戏状态新的数据表来指定总共有多少波敌人生成逻辑UI告诉当前玩家的敌人波数 3-Survival Game M…...

学习ASP.NET Core的身份认证(基于JwtBearer的身份认证9)
测试数据库中只有之前记录温湿度及烟雾值的表中数据较多,在该数据库中增加AppUser表,用于登录用户身份查询,数据库表如下所示: 项目中安装SqlSugarCore包,然后修改控制器类的登录函数及分页查询数据函数ÿ…...

缓存之美:万文详解 Caffeine 实现原理(上)
由于社区最大字数限制,本文章将分为两篇,第二篇文章为缓存之美:万文详解 Caffeine 实现原理(下) 大家好,我是 方圆。文章将采用“总-分-总”的结构对配置固定大小元素驱逐策略的 Caffeine 缓存进行介绍&…...

Spark/Kafka
文章目录 项目地址一、Spark1. RDD1.1 五大核心属性1.2 执行原理1.3 四种创建方式二、Kafka2.1 生产者(1)分区器(2)生产者提高吞吐量(3) 生产者数据可靠性数据传递语义幂等性和事务数据有序2.2 Broker(1)Broker工作流程(2)节点服役和退役2.3 副本(1)Follower故障细…...

深入浅出:Go语言中的Unicode与字符编码详解
深入浅出:Go语言中的Unicode与字符编码详解 引言 在当今的编程世界中,字符编码和Unicode是不可或缺的技术基础。Go语言作为一种强大的编程语言,其对Unicode的支持和字符编码的处理方式,对于开发者来说至关重要。本文将从Unicode的基础知识入手,逐步深入探讨Go语言中字符编…...

什么是SSL及SSL的工作流程
什么是 SSL SSL(Secure Sockets Layer,安全套接层)是一种保护互联网通信安全的加密协议,用于确保数据在客户端和服务器之间传输时的保密性、完整性和身份验证。它已被TLS(Transport Layer Security,传输层安全协议)取代,但很多场景仍习惯称其为SSL。 SSL/TLS 的主要目…...

.Net Core微服务入门全纪录(二)——Consul-服务注册与发现(上)
系列文章目录 1、.Net Core微服务入门系列(一)——项目搭建 2、.Net Core微服务入门全纪录(二)——Consul-服务注册与发现(上) 3、.Net Core微服务入门全纪录(三)——Consul-服务注…...