如何干掉Try{}Catch{}

软件开发过程中,不可避免的是需要处理各种异,所以代码中就会出现大量的try {...} catch {...} finally {...} 代码块,不仅有大量的冗余代码,而且还影响代码的可读性。

Controller 层抛出大量异常,那是相当的难看,所以尽量是在Service层处理业务时进行统一处理。不管是在哪层处理,异常肯定是需要处理,既然不能显示的处理,那只能尽量优雅着来喽。

那么怎么处理才算是优雅呢?

用Assert(断言)替换 throw Exception

使用Assert(断言)方式来校验业务的异常情况,可以只关注业务逻辑。而不用花费大量精力写荣誉的try catch 代码块。

Assert其实就是把if(...)封装了一下,虽然简单,但是不可以否认的是编码体验至少上升了一个档次。那我们也就可以仿照着

org.springframework.util.Assert 写一个断言类,不过断言失败之后抛出的是我们自定义的异常。

上代码:

如何干掉Try{}Catch{}_第1张图片

1.定义一个基础异常BaseException:

package com.cloud.producer.response;

/**
 * @Auther: Jzm
 * @Date: 2020/7/2 16:01
 * @Description:
 */
public class BaseException extends RuntimeException {

    protected IExceptionEnum iExceptionEnum;

    protected Object[] args;

    public BaseException(IExceptionEnum responseEnum) {
        super(responseEnum.getMsg());
        this.iExceptionEnum = responseEnum;
    }

    public BaseException(IExceptionEnum responseEnum, Object[] args, String message) {
        super(message);
        this.iExceptionEnum = responseEnum;
        this.args = args;

    }

    public BaseException(IExceptionEnum responseEnum, Object[] args, String message, Throwable cause) {
        super(message,cause);
        this.iExceptionEnum = responseEnum;
        this.args = args;

    }

    public IExceptionEnum getiExceptionEnum() {
        return iExceptionEnum;
    }

    public Object[] getArgs() {
        return args;
    }
}

2.业务异常BussinessException

package com.cloud.producer.response;

/**
 * @author EDZ
 * @Auther: Jzm
 * @Date: 2020/7/2 16:00
 * @Description:
 */
public class BussinessException extends BaseException{

    private static final long serialVersionUID = 1L;

    public BussinessException(IExceptionEnum responseEnum, Object[] args, String message) {
        super(responseEnum, args, message);
    }
}

3.基础断言BaseAssert

package com.cloud.producer.response;

/**
 * @Auther: Jzm
 * @Date: 2020/7/2 16:18
 * @Description:
 */
public interface BaseAssert {

    BaseException newException(Object... args);

    default void assertNotNull(Object obj){
        if(obj == null){
            throw newException(obj);
        }
    }

    default void assertNotTrue(boolean flag, Object... args) {
        if (!flag) {
            throw newException(args);
        }
    }




}

4.业务断言BussinessAssert

package com.cloud.producer.response;

import java.text.MessageFormat;

/**
 * @Auther: Jzm
 * @Date: 2020/7/2 16:24
 * @Description:
 */
public interface BusinessExceptionAssert extends IExceptionEnum,BaseAssert{

    @Override
    default BaseException newException(Object... args){
        String message = this.getMsg();
        // text.MessageFormat 定义插入可选参
        String msg = MessageFormat.format(message, args);
        if (msg.equals(message) && null != args && args.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (Object arg : args) {
                if (null == arg) {
                    continue;
                }
                sb.append(" ").append(arg);
            }
            msg = message + ":" + sb.toString();
        }
        return new BussinessException(this,args,message);
    }
}

5.通过枚举的方式自定义异常

package com.cloud.producer.response;

/**
 * @Auther: Jzm
 * @Date: 2020/7/2 15:59
 * @Description:
 */
public enum PrivateExceptionEnum implements IExceptionEnum,BusinessExceptionAssert {

    /**
     * 异常模板
     */
    PRIVATE_EXCEPTION_TEMPLATE(100, "exception msg {0}{1}");


    PrivateExceptionEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    int code;
    String msg;

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

}

6.最后还需要有一个异常基类

package com.cloud.producer.response;

import lombok.Data;

/**
 * @Auther: Jzm
 * @Date: 2020/7/2 16:04
 * @Description:
 */
public interface IExceptionEnum {

    int getCode();

    String getMsg();
}

 最后写个demo 遛一遛

package com.cloud.producer.response;

/**
 * @Auther: Jzm
 * @Date: 2020/7/2 16:28
 * @Description:
 */
public class Demo {

    public static void main(String[] args){

        PrivateExceptionEnum.PRIVATE_EXCEPTION_TEMPLATE.assertNotTrue(false,"123","456","789");
    }

}

 

你可能感兴趣的:(SpringBoot,SpringCloud,java)