【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接口。
原文地址:https://blog.csdn.net/dongjing991/article/details/145276483
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!