java优雅的处理程序中的异常

java优雅的处理程序中的异常

  捕获异常是为了处理它,不要捕获了却什么都不处理而抛弃之。。。

java异常导向图

  CheckedException和UncheckedException的区别:
  CheckedException会在编译时被检测,即要么使用try catch 代码块,要么在方法签名中用 throws 关键字声明该方法可能会抛出的CheckedException,否则编译无法通过。
  但是,CheckedException是Java中一个非常糟糕的设计,它会强迫你编写try catch来处理,大多数情况下,程序员不知道该采取什么措施进行处理,使用try catch仅仅是为了能够通过编译。
看一个反例

    @Test
    public void test02(){
            try {
                URLEncoder.encode("hello word!","UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();//!!!不要这么使用
            }
    }

  上面这个例子中,我们使用的编码是被支持的,显然不会有任何出错的信息,如果我们不适用try catch语句程序不能够正确的编译。我们可以改成一下这种方法

        try {
            URLEncoder.encode("hello word!","UTF-8");
        } catch (UnsupportedEncodingException e) {
            Logger.error("这个地方添加一些传入的参数相关信息,方面我们排查",e);
            throw new ServiceException("系统出错,请联系管理员");//自定义异常,下面会提到
        }

  Java8标准库中加入了UncheckedIOException用来避免CheckedException带来的问题,CheckedException异常除了报错,没什么可特别处理的,也处理不了,除了强迫程序员编写try catch增加工作量之外,并没有其它更多的用处。
  .在Spring框架中没有CheckedException,Controller和Service不处理任何异常,所有异常由ControllerAdvice统一处理。

一、自定义异常类

(1)继承 Exception 或 RuntimeException
(2)定义构造方法

/**
 * @author lqh
 * @date 2020/9/22
 * 业务逻辑异常
 */
public class ServiceException extends RuntimeException{

    private String code;
    private String msg;

    public ServiceException() {
    }

    public ServiceException(String msg) {
        this.msg = msg;
    }

    public ServiceException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

二、@ControllerAdvice

  @ControllerAdvice 实现全局异常处理,需配合@ExceptionHandler(注解用来指明异常的处理类型)使用。

/**
 * @author lqh
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ServerResponse exceptionHandler(Exception e) {
        return ServerResponse.error(ResponseCode.SERVER_ERROR.getMsg());
    }
    @ResponseBody
    @ExceptionHandler(ServiceException.class)
    public ServerResponse serviceExceptionHandler(ServiceException se) {
        return ServerResponse.error(se.getMsg());
    }
}

  ResponseCode是自定义枚举类,用于返回统一的状态

/**
 * @author lqh
 * @date 2020/9/21
 * 响应状态码
 */
public enum ResponseCode {

    // 系统模块
    SUCCESS(200, "操作成功"),
    SAVE_SUCCESS(201,"保存成功"),
    DELETE_SUCCESS(202,"删除成功!"),
    UPDATE_SUCCESS(403,"更新成功!"),

    ERROR(400, "操作失败"),
    SAVE_ERROR(401,"保存失败"),
    DELETE_ERROR(402,"删除失败!"),
    UPDATE_ERROR(403,"更新成功"),

    SERVER_ERROR(500, "服务器异常"),
    EXCEPTION(-1,"Exception"),

    // 用户模块 0xxxx
    NEED_LOGIN(1001, "登录失效"),
    USERNAME_OR_PASSWORD_EMPTY(1002, "用户名或密码不能为空"),
    USERNAME_OR_PASSWORD_WRONG(1003, "用户名或密码错误"),
    USER_NOT_EXISTS(1004, "用户不存在"),
    WRONG_PASSWORD(1005, "密码错误"),
}

四、异常e相关方法(解释在方法注释中)

 @Test
    public void test01(){
        try {
            System.out.println(1/0);
        }catch (Exception e){
            /**
             * 获取异常种类和错误信息
             * java.lang.ArithmeticException: / by zero
             */
            System.out.println(e.toString());

            /**
             *获取错误信息
             * / by zero
             */
            System.out.println(e.getMessage());
            /**
             * 获取异常类的Class
             *class java.lang.ArithmeticException
             */
            System.out.println(e.getClass());
            /**
             *获取异常类名称
             *java.lang.ArithmeticException
             */
            System.out.println(e.getClass().getName());
            /**
             * 会打出详细异常,异常名bai称,出错位置,便于调试用
             * java.lang.ArithmeticException: / by zero
             at com.bluewit.exception.ExceptionTest.test01(ExceptionTest.java:13)
             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
             at java.lang.reflect.Method.invoke(Method.java:498)
             at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
             at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
             at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
             at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
             at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
             at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
             at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
             at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
             at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
             at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
             at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
             at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
             at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
             at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
             at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
             at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
             at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
             at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
             */
            e.printStackTrace();

            /**
             * 返回的是通过getOurStackTrace方法获取的StackTraceElement[]数组,而这个StackTraceElement是ERROR的每一个cause by的信息。
             * [Ljava.lang.StackTraceElement;@4c203ea1
             */
            System.out.println(e.getStackTrace());

            /**
             * 返回一个包含所有被抑制的异常,通常由一个数组try -with-resources语句,为了实现这一例外。 如果没有例外被抑制或抑制被禁止 ,则返回一个空数组
             */
            System.out.println(e.getSuppressed());

            /**
             *回此异常的原因(尝试加载类时发生错误引发的异常;否则返回 null)
             * null
             */
            System.out.println(e.getCause());
        }
    }

五、自定义异常使用

(1)举例一:程序中根据业务判断抛出异常信息

	if(1=1){
		throw new ServiceException(ResponseCode.SUCCESS.getMsg());
	}

(2)举例二:根据异常信息抛出具体业务信息

 	  try {
            System.out.println(1/0);
        }catch (Exception e){
        	if(e.toString().contains("SyntaxError")){
                throw new ServiceException(ResponseCode.GRMMAR_RULES_ILLEGAL.getMsg());
            }
	   } 

六、写在最后,使用异常处理业务,而不是处理业务逻辑

异常设计的初衷是解决程序运行中的各种意外情况,且异常的处理效率比条件判断方式要低很多。

看一个反例(使用异常处理业务逻辑)

  public void processMessage(String token,RedisTemplate businessTemplate) {
    try{
        // 处理消息验证
        // 处理消息解析
        // 处理消息入库
    }catch(ValidateException e ){
        // 验证失败
    }catch(ParseException e ){
        // 解析失败
    }catch(PersistException e ){
        // 入库失败
    }
  }

  上面这个例子,很明显使用异常进行业务处理,这种方式是禁止使用的。
修改后的业务逻辑正例(使用异常处理业务)

  public void processMessage(String token,RedisTemplate businessTemplate) {
        if(StringUtils.isBlank(token)){
            throw new ServiceException(ResponseCode.ILLEGAL_ARGUMENT.getMsg());
        }
        if(!RedisUtil.hasKey(token,businessTemplate)){
            throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());
        }
        if(!RedisUtil.del(businessTemplate,token)){
            throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());
        }
 }

  上面这个例子,只管抛出异常,不做任何处理,由GlobalExceptionHandler统一处理,代码变得简洁清晰,没有太多的代码嵌套,同时所有的异常都得到了妥善的处理。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值