微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring-Exception

异常处理

  • 出现异常现象的常见位置与常见诱因如下:

    • 框架内部抛出异常:因使用不和规则导致

    • 数据层抛出异常:因外部服务器故障导致(例如:服务器访问超时)

    • 业务层抛出异常:因业务逻辑书写错误导致(遍历业务书写操作,导致索引异常等)

    • 表现层抛出异常:因数据收集,校验等股则导致(不匹配的数据类型间导致异常)

    • 工具类抛出异常:因工具类书写不够严谨不够健壮导致(必要释放的连接长期未释放)

       

  • 各个层均抛出异常,异常处理代码书写在哪一层?

    • 所有的异常均抛出到表现层进行统一处理。

  • 表现层处理异常,每个方法中单独书写,代码书写量巨大且意义不强,如何解决——AOP思想

项目异常处理方案

  • 项目异常分类

    • 业务异常(BusinessException)

      1. 规范的用户行为产生的异常

      1. 不规范的用户行为操作产生的异常

      2. *发送对应的消息传递给用户,提醒规范操作

         

    • 系统异常(SystemException)

      1. 项目运行过程中可预计且无法避免的异常

      2. *发送固定的消息传递给用户,安抚用户

        *发送特定消息给运维人员,提醒维护

        *记录日志

         

    • 其它异常(Exception)

      1. 编程人未预期到的异常

      2. **发送固定的消息传递给用户,安抚用户

        *发送特定消息给编程人员,提醒维护(纳入预期范围)

        *记录日志

Exception

package com.yang.exception;

public class SystemException extends RuntimeException{
   private Integer code;

   public SystemException(Integer code, String message, Throwable cause) {
       super(message, cause);
       this.code = code;
  }

   public SystemException(Integer code, String message) {
       super(message);
       this.code = code;
  }

   public Integer getCode() {
       return code;
  }

   public void setCode(Integer code) {
       this.code = code;
  }
}
  • BusinessException

package com.yang.exception;

public class BusinessException extends RuntimeException{
   private Integer code;

   public BusinessException(Integer code, String message, Throwable cause) {
       super(message, cause);
       this.code = code;
  }

   public BusinessException(Integer code, String message) {
       super(message);
       this.code = code;
  }

   public Integer getCode() {
       return code;
  }

   public void setCode(Integer code) {
       this.code = code;
  }
}
  • service【造的异常】

 @Override
   public Book select(Integer id) {
       //模拟自定义异常
       if (id == 1) {
           throw new BusinessException(Code.PROJECT_BUSInesS_ERROR,"请勿进行非法操作!!");
      }
       try{
           id= 1/0;
      }catch (SystemException ex){
           throw new SystemException(Code.SYstem_TIMEOUT_ERROR,"系统繁忙,请稍后重试!!");
      }
       return bookDao.select(id);
  }
  • Controller :ProjectExceptionAdvice

package com.yang.controller;

import com.yang.exception.BusinessException;
import com.yang.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {
   @ExceptionHandler(BusinessException.class)
   public Result dobusinessException(BusinessException be ){
       //发送对应的消息给用户
       return new Result(null,be.getCode(),be.getMessage());

  }
   @ExceptionHandler(SystemException.class)
   public Result doSystemException(SystemException se ){
       //发送特定消息给用户
       //发送特定消息给运维人员,提醒维护
       //记录日志
       return new Result(null,se.getCode(),se.getMessage());
  }

   @ExceptionHandler(Exception.class)
   public Result doException(Exception ex ){
       //发送特定消息给用户
       //发送特定消息给编程人员,提醒维护
       //记录日志
       return new Result(null,Code.SYstem_UNKNowN_ERROR,"系统繁忙,请联系管理员");
  }
}
  • Code

package com.yang.controller;

public class Code {
 

   public static final Integer SYstem_UNKNowN_ERROR = 50001;
   public static final Integer SYstem_TIMEOUT_ERROR = 50002;


   public static final Integer PROJECT_VALIDATE_ERROR = 60001;
   public static final Integer PROJECT_BUSInesS_ERROR = 60002;



}

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐