自定义异常,以assert的形式抛出异常
一、类关联图
文章图片
二、说明
1.通过ResponseEnum中定义的异常枚举信息,判断抛出异常;三、源码讲解
2.判断方法为公共方法,写在自定义的assert里,代码判断时调用;
【自定义异常,以assert的形式抛出异常】3.利用自定义的枚举常量直接可以引用assert里的公共方法,有异常即会抛出自己指定的异常;
文章图片
3.1异常类 3.1.1 BaseException类
package com.geo.source.exception.customassert.exception;
import com.geo.source.exception.customassert.IResponseEnum;
/**
* @author YanZhen
* @date 2020/05/08 14:25
**/
public class BaseException extends RuntimeException {
/**
* 错误码
*/
private int errorCode;
/**
* 错误信息
*/
private String errorMessage;
/**
* 传参
*/
private Object errorResult;
public BaseException(IResponseEnum resp, Object os) {
super(resp.getMessage());
this.errorCode = resp.getCode();
this.errorMessage = resp.getMessage();
this.errorResult = os;
}public int getErrorCode() {
return errorCode;
}public String getErrorMessage() {
return errorMessage;
}public Object getErrorResult() {
return errorResult;
}
}
3.1.2 BusinessException类
package com.geo.source.exception.customassert.exception;
import com.geo.source.exception.customassert.IResponseEnum;
/**
* 业务异常
*
* @author YanZhen
* @date 2020/05/08 13:48
**/
public class BusinessException extends BaseException {
private static final long serialVersionUID = 1L;
public BusinessException(IResponseEnum resp) {
super(resp, null);
}public BusinessException(IResponseEnum resp, Object os) {
super(resp, os);
}
}
3.2 assert和响应枚举 3.2.1 CustomAssert 类
package com.geo.source.exception.customassert;
import com.geo.source.exception.customassert.exception.BaseException;
/**
* 自定义断言
* @author YanZhen
* @date 2020/05/08 13:24
**/
public interface CustomAssert {
/**
* 创建异常
* @return 异常信息
*/
BaseException newException();
/**
* 创建异常
* @param args 参数
* @return 异常信息
*/
BaseException newException(Object args);
/**
* 创建异常
* @param t 异常
* @param args 参数
* @return 异常信息
*/
BaseException newException(Throwable t, Object args);
/**
* 是否为空,抛出异常
* @param o 判断对象
*/
default void assertNotNull(Object o) {
if (o == null) {
throw newException();
}
}/**
* 是否为空,抛出异常
* @param o 判断对象
* @param args 参数
*/
default void assertNotNull(Object o, Object args) {
if (o == null) {
throw newException(args);
}
}
}
3.2.2 IResponseEnum类
package com.geo.source.exception.customassert;
/**
* 响应信息枚举值模板
* @author YanZhen
* @date 2020/05/08 13:44
**/
public interface IResponseEnum {
/**
* 错误编码
* @return 错误编码
*/
int getCode();
/**
* 错误信息
* @return 错误信息
*/
String getMessage();
}
3.2.3 BusinessAssertException类
package com.geo.source.exception.customassert;
import com.geo.source.exception.customassert.exception.BaseException;
import com.geo.source.exception.customassert.exception.BusinessException;
/**
* 业务异常断言
* @author YanZhen
* @date 2020/05/08 14:07
**/
public interface BusinessAssertException extends IResponseEnum, CustomAssert {
@Override
default BaseException newException() {
return newException(null, null);
}@Override
default BaseException newException(Object args) {
return newException(null, args);
}@Override
default BaseException newException(Throwable t, Object args) {
return new BusinessException(this, args);
}
}
3.3测试
package com.geo.source.exception;
import com.geo.source.exception.customassert.exception.BusinessException;
import com.geo.source.exception.exceptinenum.ResponseEnum;
import com.geo.source.exception.simple_example.SimpleBusinessException;
import com.geo.source.exception.simple_example.SimpleResponseEnum;
/**
* @author YanZhen
* @date 2020/05/08 14:16
**/
public class TestMain {
public static void main(String[] args) {
v1();
}/**
* 异常抛出第一版
*/
private static void v1() {
try {
ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(123);
ResponseEnum.SYSTEM_EXCEPTION.assertNotNull(null, "123");
} catch (Exception e) {
BusinessException ex = (BusinessException) e;
System.out.println(ex.getErrorCode() + " : " + ex.getErrorMessage());
System.out.println("传入的参数为:" + ex.getErrorResult());
}
}
}
推荐阅读
- 爱就是希望你好好活着
- 叙述作文
- 以读攻“毒”唤新活动曹彦斌打卡第二天
- SpringBoot调用公共模块的自定义注解失效的解决
- python自定义封装带颜色的logging模块
- 拉黑家人一整年之后,以为会快乐,最后却抑郁症!!
- 靈魂裡有香氣的人
- 抱着梦的无眠
- 列出所有自定义的function和view
- 你累吗