博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义异常和异常处理
阅读量:5269 次
发布时间:2019-06-14

本文共 5154 字,大约阅读时间需要 17 分钟。

项目中一定会用到自定义异常

@Datapublic class BusinessException extends RuntimeException {    private static final long serialVersionUID = 194906846739586856L;    protected String code;    protected String message;    protected ResultCode resultCode;    protected Object data;    public BusinessException() {        ExceptionEnum exceptionEnum = ExceptionEnum.getByEClass(this.getClass());        if (exceptionEnum != null) {            resultCode = exceptionEnum.getResultCode();            code = exceptionEnum.getResultCode().getCode().toString();            message = exceptionEnum.getResultCode().getMessage();        }    }    public BusinessException(String message) {        this();        this.message = message;    }    public BusinessException(String format, Object... objects) {        this();        format = StringUtils.replace(format, "{}", "%s");        this.message = String.format(format, objects);    }    public BusinessException(String msg, Throwable cause, Object... objects) {        this();        String format = StringUtils.replace(msg, "{}", "%s");        this.message= String.format(format, objects);    }    public BusinessException(ResultCode resultCode, Object data) {        this(resultCode);        this.data = data;    }    public BusinessException(ResultCode resultCode) {        this.resultCode = resultCode;        this.code = resultCode.getCode().toString();        this.message = resultCode.getMessage();    }}

异常处理

@ControllerAdvicepublic class ExceptionHandler {    @ExceptionHandler(value = Exception.class)    @ResponseBody    public Result error(Exception e){        e.printStackTrace();        return new Result(false, StatusCode.ERROR, "执行出错");    }    @ExceptionHandler(value = BusinessException.class)    public Result errorHandle(BusinessException e){        return new Result(false, StatusCode.ERROR, e.message);    }}

枚举 ResultCode 

import lombok.Getter;@Getterpublic enum ResultCode {    PARAM_IS_INVALID(1, "参数错误"),    RESULE_DATA_NONE(2, "结果为空"),    DATA_ALREADY_EXISTED(3, "记录已存在"),    USER_NOT_LOGGED_IN(4, "用户未登录"),    PERMISSION_NO_ACCESS(5, "没有访问权限"),    INTERFACE_OUTTER_INVOKE_ERROR(6, "接口错误"),    SYSTEM_INNER_ERROR(7, "系统内部错误");    private Integer code;    private String message;    ResultCode(Integer code, String message) {        this.code = code;        this.message = message;    }}

枚举异常

public enum ExceptionEnum {    /**     * 无效参数     */    PARAMETER_INVALID(ParameterInvalidException.class, HttpStatus.BAD_REQUEST, ResultCode.PARAM_IS_INVALID),    /**     * 数据未找到     */    NOT_FOUND(DataNotFoundException.class, HttpStatus.NOT_FOUND, ResultCode.RESULE_DATA_NONE),    /**     * 数据已存在     */    CONFLICT(DataConflictException.class, HttpStatus.CONFLICT, ResultCode.DATA_ALREADY_EXISTED),    /**     * 用户未登录     */    UNAUTHORIZED(UserNotLoginException.class, HttpStatus.UNAUTHORIZED, ResultCode.USER_NOT_LOGGED_IN),    /**     * 无访问权限     */    FORBIDDEN(PermissionForbiddenException.class, HttpStatus.FORBIDDEN, ResultCode.PERMISSION_NO_ACCESS),    /**     * 远程访问时错误     */    REMOTE_ACCESS_ERROR(RemoteAccessException.class, HttpStatus.INTERNAL_SERVER_ERROR, ResultCode.INTERFACE_OUTTER_INVOKE_ERROR),    /**     * 系统内部错误     */    INTERNAL_SERVER_ERROR(InternalServerException.class, HttpStatus.INTERNAL_SERVER_ERROR, ResultCode.SYSTEM_INNER_ERROR);    private Class
eClass; private HttpStatus httpStatus; private ResultCode resultCode; ExceptionEnum(Class
eClass, HttpStatus httpStatus, ResultCode resultCode) { this.eClass = eClass; this.httpStatus = httpStatus; this.resultCode = resultCode; } public Class
getEClass() { return eClass; } public HttpStatus getHttpStatus() { return httpStatus; } public ResultCode getResultCode() { return resultCode; } public static boolean isSupportHttpStatus(int httpStatus) { for (ExceptionEnum exceptionEnum : ExceptionEnum.values()) { if (exceptionEnum.httpStatus.value() == httpStatus) { return true; } } return false; } public static boolean isSupportException(Class
z) { for (ExceptionEnum exceptionEnum : ExceptionEnum.values()) { if (exceptionEnum.eClass.equals(z)) { return true; } } return false; } public static ExceptionEnum getByHttpStatus(HttpStatus httpStatus) { if (httpStatus == null) { return null; } for (ExceptionEnum exceptionEnum : ExceptionEnum.values()) { if (httpStatus.equals(exceptionEnum.httpStatus)) { return exceptionEnum; } } return null; } public static ExceptionEnum getByEClass(Class
eClass) { if (eClass == null) { return null; } for (ExceptionEnum exceptionEnum : ExceptionEnum.values()) { if (eClass.equals(exceptionEnum.eClass)) { return exceptionEnum; } } return null; }}

用户未登录Exception异常

public class UserNotLoginException extends BusinessException {}

 

转载于:https://www.cnblogs.com/alittlesmile/p/11253914.html

你可能感兴趣的文章
深入Dagger:自定义AutoValue
查看>>
3.0 C++远征:模板
查看>>
[转][Prism]Composite Application Guidance for WPF(6)——服务
查看>>
特效:ListBox数据加载特效
查看>>
向保钓人士致敬!
查看>>
play!安装出错:Error: Could not find or load main class Files ...(2012-10-25 09:13:43)
查看>>
设计模式之禅--状态模式
查看>>
3D OpenGL ES
查看>>
怎么在两个内嵌的子网页之间通信?(已解决)
查看>>
C#实现基于ffmpeg加虹软的人脸识别demo及开发分享
查看>>
linux为用户配置java环境变量
查看>>
洛谷——P1775 古代人的难题_NOI导刊2010提高(02)&& P1936 水晶灯火灵(斐波那契数列)...
查看>>
selenium Python自动化 笔记 根据xpath找定位的响应属性 修改链接并打开
查看>>
2017-3-2 C# WindowsForm 中label标签居中显示
查看>>
javascript的use strict(使用严格模式)
查看>>
Django doc summary (7)
查看>>
What's the Agile mean
查看>>
XVII Open Cup named after E.V. Pankratiev. GP of Tatarstan B. White Triangle
查看>>
Android应用安全之脆弱的加密
查看>>
HTML5新API
查看>>